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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 26 October 2011 14:50, Allan Dixon <allan-4PljLAg39wLQT0dZR+AlfA@public.gmane.org> 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 1Probably 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Oct-26 21:12 UTC
Re: find_each(:include => ) the association never loads
On Oct 26, 5:32 pm, "Allan D." <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
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." <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Oct-27 16:29 UTC
Re: find_each(:include => ) the association never loads
On Oct 27, 12:23 am, "Allan D." <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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." <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.