0

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)
3
  • The server log should print out the database query generated from the scope, can you paste it in? Commented Aug 31, 2013 at 15:50
  • What happens when you send a blank input? Do you get all "Drawing" listed? Commented Aug 31, 2013 at 16:06
  • Yes, if I send any input, including blank, I get all Drawings listed. Commented Aug 31, 2013 at 16:07

1 Answer 1

3

The params is wrong.

Instead of

params[:description]

Use

params[:drawings][:description]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.