I have a model that works something like this. I have a User object. Each User has a Profile. Each Profile has a Picture. Well not always. At creation there is no Picture and the user could choose to never set one.

Prior to my latest upgrade of RoR I would use a line like this to check if there was a picture.

if @user.profile.picture.nil?

But when I went to 0.14.1 this started giving me an error, “uninitialized constant Picture”. I define the relationship in the model by saying:

class Picture < ActiveRecord::Base
belongs_to :profile

and in Profile

class Profile < ActiveRecord::Base
belongs_to :user

has_one :picture,

:dependent => :destroy

The addition of the :dependent => :destroy is new to this version. So how do I specify this relationship knowing the picture may not be there? Do I have to not use belongs_to, has_one?

Technorati Tags: ,

One thought on “Ruby On Rails Problem: Lazy Loading

Comments are now closed.