In my Rails app I am using collection_select to provide a list of valid choices for the user. I stored only the id from the looked-up table. I then make a call to the looked-up class in the show view to retrieve the actual value for the id.
in edit:
= f.collection_select :language_id, Language.find(:all, :conditions => ["supported = 't'"]), :id, :language, include_blank: false, :prompt => "Language"
in show:
%p
%b Language:
= Language.find(@article.language_id).language_code
This is all working fine unless no choice is made. If the @article.language_id field is null, the view cannot load as the find fails. Any suggestions on how to ignore null values and leave the field blank?
using
find_by_idwould be an option, as it does not raise an error if the id could not be found.this issues a call to the database though.
so i would just check the languange_id and provide a default
this could also be moved into the model or a helper, so that it does not clutter your view code.