Inverse Associations in Rails
Consider the following association:
class CrazyCatLady < ActiveRecord::Base has_many :cats end class Cat < ActiveRecord::Base belongs_to :crazy_cat_lady end
Now, imagine you did:
CrazyCatLady.first.cats.first
OK, this will query the database for the first CrazyCatLady, then query again for all cats associated with that record. Fair enough. But what if we do this:
CrazyCatLady.first.cats.first.crazy_cat_lady
Aside from being a bit silly, this does everything the first statement does, then queries the database a third time to find the CrazyCatLady associated with the first cat. But Rails already has this information! Why is it querying the database again?