(applies to ‘Collection_radio_buttons’ and ‘Collection_check_boxes’ as-well)
Using ‘collection_select’ on a html form in ruby can seem pretty complex, the documentation is unclear and there are not many sources to look at when trying to find a clear explanation of how it works. So i’m here to try and break it down so it is easier to understand.
Today I will explain the 5 elements of Collection_select that are required to make a collection_select. These elements look like this:
Collection_select(object, method, collection, value_method, text_method)
A simple way to look at the ‘collection_select’ elements is like this -
1. ‘Object’ - What object do we want the return value of our select to return to?
2. ‘Method’ - What attribute of our object do we want to change?
3. ‘Collection’ - What list are you selecting from?
4. ‘Value_method’ - What do you want to return from that list?
5. ‘Text_method’ - How do we want to show our list to our user?
Now that you have a simplified understanding of what the elements are asking for, let’s dive deeper to make things more clear.
Let’s start with the ‘object’ element. The ‘object’ in this case is pretty straightforward, it is the object you wish to pass the return value of the collection_select to.
For example, if you have an instance object of class Book called ‘new_book’ & you wish to change one of it’s elements using the collection_select, you would simply pass in the new_book object into the ‘object’ element like so:
@new_book = Book.newCollection_select(:new_book, method, ...)
Next, let’s talk about the ‘method’ element. The ‘method’ in this case is referring to the attribute that you wish to change from the object you passed.
Using our example from before, let’s say each instance of class Book has an attribute of a my_author and your goal is to use the ‘collection_select’ to assign a new value to my_author on your new_book. The ‘method’ element would simply be ‘:my_author’ because it is the attribute of the new_book instance that you wish to change. Example:
Collection_select(:new_book, :my_author , collection ... )
Now we are onto the ‘collection’ element. The ‘collection’ element is simply looking for the list of objects you wish to choose from.
So let’s say in our example we have our class Book and our instance new_book that we want to change or select the my_author attribute for. But now we also have an other class called Author where we have already created many instances of & want to select from those author instance objects. All we need to do is pass the array of all authors into the collection (thanks to active record, this is a pretty simple task with ‘Author.all’). So in other words the collection is the list we want to select from. Here’s the example:
Author.all #=> [author1, author2, author3, author4, ... author25]Collection_select(:new_book, :my_author , Author.all, value_method ... ]
Next is the ‘value_method’ element. This element is pretty much your return value. In other words, it is the attribute from the author instance object that you selected in Author.all that you would like to pass to your my_author attribute on new_book.
Let’s assume that each instance of the class Author has 2 attributes, a ‘name’ attribute and a object ‘id’ attribute. So when you select an author from the Author.all list, you want to assign that author’s object id to your attribute my_author on your object new_book. You would simply ask for the id of the author in the ‘value_method’ element. Like so:
Collection_select(:new_book, :my_author , Author.all, :id, text_method)
Finally, the ‘text_method’ element. This last element is what you want the user to see when selecting from the collection. You wouldn’t want the user to see a list of the author’s id numbers when selecting the authors, you would want the user to select the author by their name. So this is where you would put the author instance object’s attribute called ‘name’.
So your final result would be:
Collection_select(:new_book, :my_author , Author.all, :id, :name)
Now let’s answer the questions that we asked in the beginning -
1. ‘Object’ - What object do we want the return value of our select to go? Our object ‘new_book’
2. ‘Method’ - What part of our object do we want to change? The attribute on object ‘new_book’ called ‘my_author’
3. ‘Collection’ - What list are you selecting from? In our case, the Author.all list (list of all author objects).
4. ‘Value_method’ - What do you want to return from that list? In our case the author object’s attribute called ‘id’.
5. ‘Text_method’ - How do we want to show show our list to our user? Through our attribute ‘name’