Rails: find_each(:include => :user ) 永远不会获得关联
我的控制器中有两个模型
class User
has_one :entry
end
class Entry
belongs_to: user
end
,我使用 find_each 迭代条目以向每个用户发送电子邮件。
Entry.find_each(:include => :user, :conditions => {:approved => true}) do |entry|
UserMailer.send_competition_open_email(entry, entry.user)
end
entry.user 始终为nil。 “:include => :user”永远找不到用户。
但我可以在我的 SQL 日志中看到它试图获取它。但失败了。有什么想法吗?
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
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. ":include => :user" never finds the user.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需快速检查一下您是否注意到冒号对接的是“belongs_to”而不是“user”?
Just a quick check that you noticed the colon is butting "belongs_to" and not "user"?