通过has_many列出

时间:2013-04-30 14:48:16

标签: ruby-on-rails activerecord

我有这个数据库架构

class User
  has_many :accounts
end

class Account
  belongs_to :user
  belongs_to :biller
end

class Biller
  has_many :accounts
end

如何获取用户的记分员列表?

billers = user.?

1 个答案:

答案 0 :(得分:1)

使用完整选项添加has_many关联:

class User
  has_many :accounts
  has_many :billers, through: :accounts 
end

class Account
  belongs_to :user
  belongs_to :biller
end

class Biller
  has_many :accounts
end

并使用它如下:

billers = user.billers

更多信息请参阅活动记录guide