rails has_many through scope

时间:2012-09-11 22:13:47

标签: ruby-on-rails scope has-many-through

我正在通过Company表格链接UserAssociation模型:

class User < ActiveRecord::Base
  has_many :associations
  has_many :companies, :through => :associations

class Company < ActiveRecord::Base
  has_many :associations
  has_many :users, :through => :associations

在我的协会中,我有一个名为active的布尔列:

create_table "associations", :id => false, :force => true do |t|
    t.integer  "company_id"
    t.integer  "user_id"
    t.boolean  "active"

在我的公司控制器中,我正在尝试获取一个公司阵列,其中包括用户已被激活或未被激活的事实。

我设法使用了一个范围,但这只是为了过滤公司是否有效,而不是关联:

@companiesactive = @user.companies.by_status(true)

希望获得类似的结果:

@companiesuseractive = @user.companies.where("association.active", true)

非常感谢任何帮助。

0 个答案:

没有答案