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

On 26 October 2011 14:50, Allan D. [email protected] wrote:

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

On Oct 26, 5:32pm, “Allan D.” [email protected] wrote:

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 C. wrote in post #1028696:

On Oct 26, 5:32pm, “Allan D.” [email protected] wrote:

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

On Oct 27, 12:23am, “Allan D.” [email protected] wrote:

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

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