I want to override/extend a Rails has_one
association method so that it always returns an instance of the associated class. (If none already exists in the database, I want to create a new one and assign it to the parent instance.)
Ideally, I'd like to do this through the built-in Rails association extension mechanism. However, I don't know the name of the "getter" method, and so I don't know what to override.
How do I override the association getter so that I can instantiate a new object when it's nil?
new
action do something like@parent = Parent.new(:child => Child.new)
. In theedit
action do@parent = Parent.find(params[:id])
then@parent.child = Child.new unless @parent.child.present?
. Something like that. – Grison