Wednesday, October 26, 2011

Re: [Rails] find_each(:include => ) the association never loads

On 26 October 2011 14:50, Allan Dixon <allan@buttrr.com> 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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate