A colleague just raised an interesting question, “How do you set default options for your ActiveRecord objects?” After a bit of pondering we realized that there isn’t an easy way to do this, so I headed off to the Rails Plugin Repository looking for a quick win. It appears that we weren’t the only people to have this problem, and fortunately, the problem has already been solved for us. The ActiveRecordDefaults plugin is going to get a special place in my arsenal of tools for hacking on Ruby.
<p>Thanks for the great implementation <a href="http://agilewebdevelopment.com/plugins/owner/119">Jonathon Viney</a>!</p>
<p>I’m borrowing this example from the plugin’s project page:</p>
1 2 3 4 5 6 7 |
class Person defaults :country => 'New Zealand', :type => 'Unknown' default :last_name do |person| person.first_name end end |