gpt4 book ai didi

ruby-on-rails - 使用acts_as_taggable_on 进行搜索

转载 作者:行者123 更新时间:2023-12-03 02:13:08 25 4
gpt4 key购买 nike

我正在尝试在模型中搜索具有特定标签的所有记录。

我们尝试搜索的示例输出:

ruby-1.9.2-p0 :1 > Question.last.tags
=> [#<ActsAsTaggableOn::Tag id: 2, name: "preferences">, #<ActsAsTaggableOn::Tag id: 13, name: "travel">]

所以,Questiontags例如偏好和旅行。我尝试过:

@tags = @questions.where("tags LIKE ?", "%#{params[:tags]}%")
=> []

@tags = @questions.where("tag LIKE ?", "%#{params[:tags]}%")
=> []

@tags = @questions.where("tag_list LIKE ?", "%#{params[:tags]}%")
=> BadFieldError: Unknown column 'tag_list'

params[:tags]时如何返回顶部记录例如,是“refere”还是“ences”?

问题模型

class Question < ActiveRecord::Base
has_many ...
acts_as_taggable_on :tags
end

第二次尝试

我刚刚创建了一个自定义 Controller 操作来尝试按照 @Nash 的建议更改查询。我该如何纠正它?

def autocomplete_question_tags
@tags = Question.tagged_with("%#{params[:term]}", :any => true)
respond_to do |format|
format.json
end
end

在表单中输入“旅行”后:

Started GET "/answers/autocomplete_question_tags?term=travel" for 127.0.0.1 at 2011-04-16 20:37:34 -0400<br/>
Processing by AnswersController#autocomplete_question_tags as JSON<br/>
Parameters: {"term"=>"travel"}<br/>
{"term"=>"travel", "action"=>"autocomplete_question_tags", "controller"=>"answers"}<br/>
SQL (1.8ms) SHOW TABLES<br/>
SQL (0.8ms) SELECT COUNT(*) AS count_id FROM (SELECT 1 FROM 'questions' WHERE (questions.id IN (SELECT taggings.taggable_id FROM taggings JOIN tags ON taggings.tag_id = tags.id AND (tags.name LIKE '%travel') WHERE taggings.taggable_type = 'Question'))) AS subquery<br/>
logger.debug: 1 result in query<br/>
Completed in 114ms

ActionView::MissingTemplate (Missing template answers/autocomplete_question_tags with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml, :haml], :formats=>[:json], :locale=>[:en, :en]} in view paths "/Users/san/Documents/san/apl/app/views",<br/>
app/controllers/answers_controller.rb:36:in 'autocomplete_question_tags'

Rendered /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (0.7ms)

最佳答案

您可以允许参数搜索标签的任何部分。

Question.tagged_with("%#{params[:tags]}%", :any => true)

关于ruby-on-rails - 使用acts_as_taggable_on 进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5685773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com