:has_one, :idiot

Post by: on February 19, 2011 | 0 Comment

So, I just learned the following:


Specifies a one-to-one association with another class. This method should only be used if the other class contains the foreign key.

I had thought, erroneously, that when you created a :has_one relationship, that you also set :belongs_to on the associated object. This is often shown in examples – but it only works of you’ve set a foreign key field on both Models. I only had it on one, so my code kept breaking trying to access the associated model from the main model. So it comes down to whether you use :has_one or :belongs_to entirely depends on which one of them has the key. At least it’s working now.