Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a rails scope query in a controller
    primarykey
    data
    text
    <p>I'm working on my first rails project, and I'm having trouble getting my query to work correctly. My overall goal is to setup a collection of scopes to create some long queries with multiple options. However, to start with, I'm just trying to set up a single scope, to query a single column from a single model. Here is my model:</p> <pre><code> class Drawing &lt; ActiveRecord::Base has_many :revisions scope :by_description, lambda { |description| where(description: description) unless description.nil? } end </code></pre> <p>Here is the index action of my controller:</p> <pre><code>def index @drawings = Drawing.by_description(params[:description]).all respond_to do |format| format.html # index.html.erb format.json { render json: @drawings } end end </code></pre> <p>And the view is the default index view for rails.</p> <p>When I enter an input into the search field, for example "Case", the submit button works and directs me to the index view, but all of the "Drawings" are listed, instead of just the ones that have "Case" as their description. Thanks so much for your help.</p> <p>Here is the section of the development log file that applies, as you can see, the SQL being generated is not including the added description parameter:</p> <pre><code>Started GET "/drawings?utf8=%E2%9C%93&amp;drawings%5Bdescription%5D=Case&amp;drawings%5Bdrawing_number%5D=&amp;drawings%5Bitem_number%5D=&amp;drawings%5Bpump_model%5D=&amp;drawings%5Bframe_size%5D=&amp;drawings%5Bpart_type%5D=&amp;drawings%5Bcreated_before%281i%29%5D=&amp;drawings%5Bcreated_before%282i%29%5D=&amp;drawings%5Bcreated_before%283i%29%5D=&amp;drawings%5Bcreated_after%281i%29%5D=&amp;drawings%5Bcreated_after%282i%29%5D=&amp;drawings%5Bcreated_after%283i%29%5D=&amp;commit=Search" for 127.6.43.1 at 2013-08-31 11:39:28 -0400 Processing by DrawingsController#index as HTML Parameters: {"utf8"=&gt;"✓", "drawings"=&gt;{"description"=&gt;"Case", "drawing_number"=&gt;"", "item_number"=&gt;"", "pump_model"=&gt;"", "frame_size"=&gt;"", "part_type"=&gt;"", "created_before(1i)"=&gt;"", "created_before(2i)"=&gt;"", "created_before(3i)"=&gt;"", "created_after(1i)"=&gt;"", "created_after(2i)"=&gt;"", "created_after(3i)"=&gt;""}, "commit"=&gt;"Search"} [1m[36mDrawing Load (0.3ms)[0m [1mSELECT "drawings".* FROM "drawings" [0m Rendered drawings/index.html.erb within layouts/application (6.0ms) Completed 200 OK in 226ms (Views: 186.8ms | ActiveRecord: 0.3ms) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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