Reputation: 1128
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:
class Drawing < ActiveRecord::Base
has_many :revisions
scope :by_description, lambda { |description| where(description: description) unless description.nil? }
end
Here is the index action of my controller:
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
And the view is the default index view for rails.
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.
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:
Started GET "/drawings?utf8=%E2%9C%93&drawings%5Bdescription%5D=Case&drawings%5Bdrawing_number%5D=&drawings%5Bitem_number%5D=&drawings%5Bpump_model%5D=&drawings%5Bframe_size%5D=&drawings%5Bpart_type%5D=&drawings%5Bcreated_before%281i%29%5D=&drawings%5Bcreated_before%282i%29%5D=&drawings%5Bcreated_before%283i%29%5D=&drawings%5Bcreated_after%281i%29%5D=&drawings%5Bcreated_after%282i%29%5D=&drawings%5Bcreated_after%283i%29%5D=&commit=Search" for 127.6.43.1 at 2013-08-31 11:39:28 -0400
Processing by DrawingsController#index as HTML
Parameters: {"utf8"=>"✓", "drawings"=>{"description"=>"Case", "drawing_number"=>"", "item_number"=>"", "pump_model"=>"", "frame_size"=>"", "part_type"=>"", "created_before(1i)"=>"", "created_before(2i)"=>"", "created_before(3i)"=>"", "created_after(1i)"=>"", "created_after(2i)"=>"", "created_after(3i)"=>""}, "commit"=>"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)
Upvotes: 0
Views: 4210
Reputation: 24815
The params is wrong.
Instead of
params[:description]
Use
params[:drawings][:description]
Upvotes: 3