Conditions parameter for find

I would like to limit the results returned by the category_id. I can manually insert the category_id :conditions => ['category_id = 1'] but I would like it to respond to the input from the search form instead.

:conditions => ["category_id like ?", params[:category_id]+"%"] :conditions => ["category_id ?"]

I can't figure this one out. The view is:

<% form_tag search_dudes_path do -%> <p>     <%= collection_select(:dude, :category_id, Category.all, :id, :name) %> </p> . . .

Sean Six wrote:

  I would like to limit the results returned by the category_id. I can manually insert the category_id :conditions => ['category_id = 1'] but I would like it to respond to the input from the search form instead.

:conditions => ["category_id like ?", params[:category_id]+"%"] :conditions => ["category_id ?"]

I can't figure this one out. The view is:

<% form_tag search_dudes_path do -%> <p>     <%= collection_select(:dude, :category_id, Category.all, :id, :name) %> </p> . . .

Why are you using LIKE with a (presumably numeric) ID?

Best,

It seems most reasonable to me that it would be :conditions => ["category_id = ?", category_id]

But that gives me an error.

undefined local variable or method `category_id' for #<DudesController:0xb6eb3960>

[Please quote properly in your replies.]

Sean Six wrote:

It seems most reasonable to me that it would be :conditions => ["category_id = ?", category_id]

But that gives me an error.

undefined local variable or method `category_id' for #<DudesController:0xb6eb3960>

So don't use an undefined variable. This is a Ruby error. It has nothing to do with your SQL query string.

Best,

category_id = params[:category_id]

@dudes = Dude.find(:all, :conditions => ["category_id = ?", category_id])

This is returning no results.

Alight. :conditions => ["category_id = :category_id", params[:dude]]

Sean Six wrote:

category_id = params[:category_id]

@dudes = Dude.find(:all, :conditions => ["category_id = ?", category_id])

This is returning no results.

Then there are no results to return. This is the correct syntax.

Best,

You can also use: @dudes = Dude.find(:all, :conditions => {:category_id => category_id})