find_each(:include => ) the association never loads

I have two models

class User
  has_one :entry
end

class Entry
  belongs_to: user
end

in my controller I use find_each to iterate over entries to email each
of the users.

    Entry.find_each(:include => :user, :conditions => {:approved =>
true}) do |entry|
      UserMailer.send_competition_open_email(entry, entry.user)
    end

entry.user is always nil..

yet i can see in my SQL logs it tries to get it. But fails. Any ideas?

Entry Load (0.6ms) SELECT `entries`.* FROM `entries` WHERE
`entries`.`approved` = 1 AND (`entries`.`id` >= 0) ORDER BY
`entries`.`id` ASC LIMIT 1000
  User Load (1.4ms) SELECT `users`.* FROM `users` WHERE (`users`.`id`
IN (1,2,3))
  User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id`
= 1 LIMIT 1

Probably one or more of your entries does not have an associated user.
Put in some diagnostic code that checks for entry.user nil and
displays the entry data, including id. Then have a look at all your
users and see if there is one that has that value in user_id.

Colin

Thanks, But no luck,

I checked all the users and entries, they all match up with id's and are
not null.

I then proceeded to delete all but one entry/user.

They are both associated but still it never gets the user object.

I put "attr_accessible :user_id, :user" on the entry table. But still no
luck.
I just can't load a has_one association

Thanks, But no luck,

I checked all the users and entries, they all match up with id's and are
not null.

I then proceeded to delete all but one entry/user.

They are both associated but still it never gets the user object.

I put "attr_accessible :user_id, :user" on the entry table. But still no
luck.
I just can't load a has_one association

So is entry.user non nil if you don't use the include option ?

Fred

Yes its Nil, nothing loads,

Does it have something to do with Devise running as the authentication?

I'm baffled why even a simple Entry.find(:include => :user) won't work
either

Frederick Cheung wrote in post #1028696:

Yes its Nil, nothing loads,

so then it's unlikely to be anything to do with :include. You can see
it doing stuff like
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1

in your logs - if you run that query outside of rails do you get a row
back?

if that gets you a row and/or if User.find(some_entry.user_id) gets
rows back then i'd whip out the debugger and step into the user
accessor method to see what is happening

Fred