Rails:find_each(:include =>:user)永远不会获得关联

时间:2011-10-26 13:05:05

标签: ruby-on-rails-3 include find associations

我有两个型号

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

1 个答案:

答案 0 :(得分:4)

class Entry
  belongs_to: user
end

只是快速检查一下你是否注意到冒号是“belongs_to”而不是“用户”?