My initial thought when trying to make use of collection_select in a form was: You mean I have to memorize something? Why can’t it just be logical and easy? As a rookie Rubyist, I want everything to look as close to English as possible, and this didn’t fit. In my first months of programming, I’ve come to understand the importance of relationships, and not the arbitrary ordering of items (or in this case, arguments). But as the old saying goes, there are two certainties in life: death and syntax. Or something like that.
collection_select is a method programmers use in creating forms. The advantage in using this method as opposed to a text input is that a user can only select from pre-existing values, which makes things easier on the user and keeps our table clean by preventing the creation of duplicate or unnecessary columns in error.
Here’s the format we’re working with:
collection_select(object, method, collection, value_method,
text_method, options = {}, html_options = {})
Let’s start with the first thing we see in parentheses: object
The object parameter here is referring to the instance of a class whose attribute you are selecting a value for. In the example below, the attribute value being selected will be assigned to the :employee (object) the user is creating a record for.
collection_select(:employee, :department_id, Department.all, :id,:name)
Note: When using FormBuilders form_with or form_for, this will be the target model and therefore won’t need to be included as an argument. Here, the dropdown created by collection_select could serve the purpose of selecting a department the employee will be working in (or updating an existing employee record if they’ve transferred to another). E.g.:
<%= form_for @employee do |f| %>
<%= f.collection_select(:department_id, Department.all, :id, :name) %>
<%= f.submit %>
<% end %>
Next, we have the method.
Here, the method refers to the attribute of the object that the dropdown value must correspond to. (Read: foreign key!!) This would be the :department_id in the above examples.
Following the method, another method! This one’s called the value method.
The value method will be called on each object instance (or row of a table, if we’re thinking in terms of a database) to retrieve a value. Often, this will be the a primary key of the associated object. Continuing with the above example, we’d locate unique department instances by their :id.
The last thing I’ll be covering is yet another method: the text method.
This appears as visible text the user can see as a selection value within the dropdown. Typically, this is a :name that is easily identifiable to the person making the selection. The above example follows this convention.
Although the last arguments can provide many options for customization of a collection_select dropdown, they are completely optional, and thus out of scope for the purposes of this post. If you’d like to know more, I’d suggest you check out this link:
Rails API: Form Options Helper

