I have not seen this before, but decided to give him a chance.
First, I used the owner association method in the Cat model to keep a backup of the original method. I tried the owner method to call the build_owner method (returns a new owner object through an association) if the original method returns nil. Otherwise, return the original_owner_method object.
class Cat < ActiveRecord::Base has_one :owner alias :original_owner_method :owner def owner if original_owner_method.nil? build_owner else original_owner_method end end
So if you call: cat = Cat.first
Assuming it has no owner, it will create a new Owner object when called: cat.owner.name
It will return zero, but it will still build the owner object in the cat.owner part of the chain without calling the missing method.
source share