six
six

Reputation: 11

Find by category_id

In my search form I have a collection_select feature for picking a category.

<% form_tag search_places_path do -%>
   <%= collection_select(:place, :category_id, Category.all, :id, :name) %>

When I try this in the controller:

 @places = Place.find(:all, 
                  :conditions => ["category_id = ?", params[:category_id]])

I am seeing category_id = null in the search log.

When I try-

@places = Place.find(:all, 
                  :conditions => ["category_id = ?", params[:place]])

I get (((category_id = '--- \n- category_id\n- \"1\"\n') in the search log.

What is the proper conditional statement here?

Upvotes: 1

Views: 225

Answers (1)

Doug Neiner
Doug Neiner

Reputation: 66231

Since category_id is coming in "nested" in the place object, you need to retrieve it using params[:place][:category_id]:

@places = Place.all(:conditions => ["category_id = ?", params[:place][:category_id] ])

Upvotes: 2

Related Questions