collection_select

Dear Ruby on Rails Community,

I am writing to propose a very minor change to the Rails framework. The helper method collection_select under Helpers::FormOptionsHelper is used to generate a <select> tag with relevant <options> from a given Collection, as you all know. This method's current parameters are as follows:

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

In the spirit of common naming conventions, I reckon the value_method and text_method parameters should default to :id and :name respectively. What are everyone's views on this?

Regards,

Edd Morgan http://www.eddm.co.uk

In the spirit of common naming conventions, I reckon the value_method and text_method parameters should default to :id and :name

respectively. What are everyone’s views on this?

While those are very reasonable defaults it seems like the error message would have to be quite elaborate in case the model doesn’t have a :name attribute. I think it would be quite difficult for a newbie to write:

<%= collection_select :user, :login, @users %> and try to debug: NoMethodError: undefined method `name’ for #User:0x340948c

Given that ‘name’ doesn’t appear in their code the average developer would be confused.

::Jack Danger

That's a good point, but I remember as a developer starting with rails I found this was true of a lot of things. Often the solution was just that I was not following standardized naming (<singular>_id foreign key for model associations, for example). Because of this, though, I started to follow standard naming and embrace the importance of it and became a more effective developer because of it.

This is of course a very minimal thing that I considered while working with it today and was just interested as to what everyone thought of it and how deep these conventions should go.

Regards, Edd Morgan http://www.eddm.co.uk

In the spirit of common naming conventions, I reckon the value_method and text_method parameters should default to :id and :name respectively. What are everyone's views on this?

Hmmm, well, I mostly use "description" rather than "name" in my lookup tables... And since these arguments come before the options and html_options, you'd have to specify them anyway if you wanted to set options such as a blank line or prompt at the top of the list, or to set a class name, for example. So it wouldn't be much use to me!

Rebecca