Eager loading of 'is meant to be nil' Associations

If I ‘find’ using :include, and some included associations are null,
then looping through the array executing ‘if obj[i].assoc’ accesses the
database for a second time (as obj.assoc is nil).

eg
Topic, has_many posts
Post has_one email

and

posts = Post.find(:all, :condition => ‘posts.topic_id = 10’, :include =>
[‘email’])

then

for post in posts do
if post.email
#do something here
end
end

and

some post objects do not have an email address set.

then, as post.email is nil active record attempts to load the email
address, which of course is nil.

Is there a way to ‘fix’ or ‘freeze’ the post object so that it Active
Record won’t attempt to access the DB for a second time?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs