I have a table that includes a “belongs to” in the model.
The table includes the xx_id field to link the two tables.
But, sometimes the xx_id is going to be blank.
When it is, I get ActiveRecord::RecordNotFound.
I don’t want an error – I just want a blank display for this field.
What do you suggest?
Rails will always raise an ActiveRecord::RecordNotFound exception when you use the
findmethod. Thefind_by_*methods, however, returnnilwhen no record is found.The ActiveRecord documentation tells us:
If you’d like to return
nilwhen records cannot be found, simply handle the exception as follows: