# Rails collection\_select syntax issue

**URL:** <https://www.ruby-forum.com/t/rails-collection-select-syntax-issue/169060>\
**Category:** Rails\
**Created:** [May 28, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/rails-collection-select-syntax-issue/169060 "2009-05-28T21:24:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ReidO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@ReidO](https://www.ruby-forum.com/u/ReidO)\
**Post date:** [May 28, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/rails-collection-select-syntax-issue/169060/1 "2009-05-28T21:24:15Z")

</div>

I’m trying to display unique counties listed in my database in select  
box for a property database. I’ve figured out how to do this, but now  
I can’t figure out how to access the selected value of the select.  
This mainly has do with the way the HTML select name is outputted.

My form code, county is an attribute for my property model:

\<%= collection\_select(:property, :county,  
@Counties, :county, :county, {:prompt =\> true}) %\>

This outputs the HTML

Please select

Pearl River Marion Stone Lamar Forrest Jones Washington

It is the []'s in the select name "property[county] that is giving me  
fits. The other items in the search form use select\_tag so the output  
is simply “min\_price” rather than “property[min\_price]”. This is  
causing a syntax error when I’m trying to put together my search  
results array in my Property model:

def self.find\_by\_lcc(params)  
where = []  
unless params[:mls].blank?  
where \<\< “mls = :mls”  
end  
unless params[:county].blank?  
where \<\< “county = :county”  
end  
unless params[:min\_acreage].blank?  
where \<\< “acreage \>= :min\_acreage”  
end  
unless params[:max\_acreage].blank?  
where \<\< “acreage \<= :max\_acreage”  
end  
unless params[:min\_price].blank?  
where \<\< “price \>= :min\_price”  
end  
unless params[:max\_price].blank?  
where \<\< “price \<= :max\_price”  
end

if where.empty?  
[]  
else  
find(:all,  
:conditions =\> [where.join(" AND "), params],  
:order =\> “city, price desc”)  
end

Due to the county problem all the records are being listed rather than  
just the properties within that county. The browser URL string I’m  
getting is:

public/land?mls=&property[county]  
=Stone&min\_acreage=0&max\_acreage=16000000&min\_price=0&max\_price=1600000&commit=Search

I have searched for answers on this for a couple of days and I’m sure  
it’s a simple syntax method I need to use in compiling my search  
array.

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![ReidO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@ReidO](https://www.ruby-forum.com/u/ReidO)\
**Post date:** [May 30, 2009, 1:27am UTC](https://www.ruby-forum.com/t/rails-collection-select-syntax-issue/169060/2 "2009-05-30T01:27:34Z")

</div>

You’re probably better off using select\_tag, together with  
options\_for\_select here. collection\_select appears to be giving you a  
bit more magic than you want…

Example:

\<%= select\_tag :county, options\_for\_select([“Please select a  
county…”, “”]removed\_email\_address@domain.invalid { |c| [c,c]}) %\>

(change the entries in the map call if @counties has objects rather  
than strings - the first entry in the array is the label, and the  
second is the value that will be sent)

–Matt J.

---

<div class="post-metadata">

**Author:** ![ReidO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@ReidO](https://www.ruby-forum.com/u/ReidO)\
**Post date:** [May 31, 2009, 5:34am UTC](https://www.ruby-forum.com/t/rails-collection-select-syntax-issue/169060/3 "2009-05-31T05:34:34Z")

</div>

Thank you Matt! This worked nicely.
