Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use?

I can't see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a scenario where collection_select is better than select? or is anything I am missing here?

like image 344
webnuwan Avatar asked Nov 13 '09 11:11

webnuwan


2 Answers

collection_select is intended to be used when the list of items is an array of ActiveRecord objects. collection_select is built on the top of select so it's a convenient method when you need to display a collection of objects and not an array of strings.

collection_select(:post, :author_id, Author.find(:all), :id, :name)
like image 96
Simone Carletti Avatar answered Sep 29 '22 10:09

Simone Carletti


I have written something on that a while back, have a look at http://nasir.wordpress.com/2007/11/02/not-binding-your-selection-list-to-a-particular-model-in-rails/

Hope that helps

like image 28
nas Avatar answered Sep 29 '22 11:09

nas