Note that there are some explanatory texts on larger screens.

plurals
  1. POSearching with acts_as_taggable_on
    text
    copied!<p>I'm trying to search through a model for all records with a certain tag.</p> <p>Sample output we are trying to search:</p> <pre><code>ruby-1.9.2-p0 :1 &gt; Question.last.tags =&gt; [#&lt;ActsAsTaggableOn::Tag id: 2, name: "preferences"&gt;, #&lt;ActsAsTaggableOn::Tag id: 13, name: "travel"&gt;] </code></pre> <p>So, <code>Question</code> has <code>tags</code> such as preferences and travel. I tried:</p> <pre><code>@tags = @questions.where("tags LIKE ?", "%#{params[:tags]}%") =&gt; [] @tags = @questions.where("tag LIKE ?", "%#{params[:tags]}%") =&gt; [] @tags = @questions.where("tag_list LIKE ?", "%#{params[:tags]}%") =&gt; BadFieldError: Unknown column 'tag_list' </code></pre> <p>How can I return the top record when <code>params[:tags]</code> is, for example, "refere" or "ences"?</p> <h3>Question model</h3> <pre><code>class Question &lt; ActiveRecord::Base has_many ... acts_as_taggable_on :tags end </code></pre> <h2>Second Try</h2> <p>I just created a custom controller action to try to change the query as per <b>@Nash</b>'s suggestion. How can I correct it?</p> <pre><code>def autocomplete_question_tags @tags = Question.tagged_with("%#{params[:term]}", :any =&gt; true) respond_to do |format| format.json end end </code></pre> <p>Upon entering 'travel' into the form:</p> <p><code>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"=&gt;"travel"}<br> {"term"=&gt;"travel", "action"=&gt;"autocomplete_question_tags", "controller"=&gt;"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</code></p> <p><code>ActionView::MissingTemplate (Missing template answers/autocomplete_question_tags with {:handlers=&gt;[:erb, :rjs, :builder, :rhtml, :rxml, :haml], :formats=&gt;[:json], :locale=&gt;[:en, :en]} in view paths "/Users/san/Documents/san/apl/app/views",<br> app/controllers/answers_controller.rb:36:in 'autocomplete_question_tags'</code></p> <p><code>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)</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload