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


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Colin Law at Oct 26, 2011 at 2:49 pm

    On 26 October 2011 14:50, Allan Dixon wrote:
    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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Allan D. at Oct 26, 2011 at 4:32 pm
    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

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Frederick Cheung at Oct 26, 2011 at 9:12 pm

    On Oct 26, 5:32 pm, "Allan D." wrote:
    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
    --
    Posted viahttp://www.ruby-forum.com/.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Allan D. at Oct 26, 2011 at 11:23 pm
    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:
    On Oct 26, 5:32pm, "Allan D." 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
    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Frederick Cheung at Oct 27, 2011 at 4:31 pm

    On Oct 27, 12:23 am, "Allan D." 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

    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:
    On Oct 26, 5:32pm, "Allan D." 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
    --
    Posted viahttp://www.ruby-forum.com/.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedOct 26, '11 at 2:40p
activeOct 27, '11 at 4:31p
posts6
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase