Searching with acts_as_taggable_on
Asked Answered
F

2

5

I'm trying to search through a model for all records with a certain tag.

Sample output we are trying to search:

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

So, Question has tags such as preferences and travel. I tried:

@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'

How can I return the top record when params[:tags] is, for example, "refere" or "ences"?

Question model

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

Second Try

I just created a custom controller action to try to change the query as per @Nash's suggestion. How can I correct it?

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

Upon entering 'travel' into the form:

Started GET "/answers/autocomplete_question_tags?term=travel" for 127.0.0.1 at 2011-04-16 20:37:34 -0400
Processing by AnswersController#autocomplete_question_tags as JSON
Parameters: {"term"=>"travel"}
{"term"=>"travel", "action"=>"autocomplete_question_tags", "controller"=>"answers"}
SQL (1.8ms) SHOW TABLES
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
logger.debug: 1 result in query
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",
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)

Flaky answered 16/4, 2011 at 10:3 Comment(2)
could you show us your Question model? And why do you have tags or tag column in your questions table?Kennel
@nash - Sure, I'll post the question model. I have tags in the questions table because I want questions to be tagged, as per the example at github.com/mbleigh/acts-as-taggable-on, where the User table is tagged on three fields.Flaky
K
4

You could allow your parameter to search any part of a tag instead.

Question.tagged_with("%#{params[:tags]}%", :any => true)
Kennel answered 16/4, 2011 at 14:28 Comment(2)
the controller action (and therefore the query) isn't specified by me - it's generated automatically by acts_as_taggable_on. How can I modify the query it automatically generates to what you suggested?Flaky
At first, you can use acts_as_taggable instead of acts_as_taggable_on :tags. And what do yo mean generated automatically by acts_as_taggable_on? As I know, acts_as_taggable_on can generate migration only. Secondly, you should use "%#{params[:term]}#" instead of "%#{params[:term]}" if you want to find something like "refere". And in your controller pass your found questions into view format.json => @tagsKennel
B
4

The accepted answer no longer works.

The current solution is:

Question.tagged_with(params[:tags], wild: true, any: true)
Bunnie answered 7/1, 2017 at 1:58 Comment(1)
Checked with version 4.0.0 of ActsAsTaggableOn: github.com/mbleigh/acts-as-taggable-on#finding-tagged-objectsBunnie

© 2022 - 2024 — McMap. All rights reserved.