通过另一个计算资源?

时间:2012-08-29 16:09:16

标签: ruby-on-rails associations

这是我的关系区域:

Account has_many :emails
Email has_many :recipients
Email belongs_to :account
Recipient belongs_to :email

我想要做的是计算任何给定帐户的收件人数。

1 个答案:

答案 0 :(得分:1)

您需要在:through模型中添加Account关系,如下所示:

class Account
  has_many :emails
  has_many :recipients, :through => :emails
end

然后你可以这样做:

Account.first.recipients.count

希望有所帮助