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 -%>

<%= collection_select(:dude, :category_id, Category.all, :id, :name) %>

. . .

Sean S. 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 -%>

<%= collection_select(:dude, :category_id, Category.all, :id, :name) %>

. . .

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

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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 S. 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,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

category_id = params[:category_id]

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

This is returning no results.

Sean S. 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,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs