如何在Rails中的另一个表上为条件创建has_many_and_belongs_to关联?

时间:2014-05-03 19:29:49

标签: ruby-on-rails ruby ruby-on-rails-3 devise associations

我有User模型的用户表,该表是通过角色模型的设计,角色表创建的。 UserRole都与HABTM相关联。

roles:
id: integer
name: string

roles_users:
user_id:integer
roles_id:integer

我有三个角色:管理员,经理和记者。

现在,我创建一个Team模型,如下所示:

teams:
name: string

我想为一个团队配备很多经理。

所以,如果我创建一个团队:

t = Team.create(name: 'Football')

然后我想获取所有管理团队' Football'的经理(有角色的用户:'经理')。所以,我在Team和User之间创建了HABTM。

teams_users:
team_id:integer
user_id:integer

我在managers模型中需要一个关联:Team。但是,到目前为止,这段代码还没有起作用:

class Team < ActiveRecord::Base
  attr_accessible :name

  validates :name, presence: true

  has_and_belongs_to_many :managers, class_name: 'User', association_foreign_key: 'user_id', conditions: joins(:roles, :roles_users).where('roles_users.role_id = ?', Role.select('id, name').find_by_name('manager').id)

end

当我这样做时:t.managers我收到此错误:

ActiveRecord::ConfigurationError: Association named 'roles' was not found on Team; perhaps you misspelled it?
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/join_dependency.rb:112:in `build'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/join_dependency.rb:123:in `block in build'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/join_dependency.rb:122:in `each'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/join_dependency.rb:122:in `build'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/join_dependency.rb:18:in `initialize'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/relation/query_methods.rb:358:in `new'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/relation/query_methods.rb:358:in `build_joins'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/relation/query_methods.rb:266:in `build_arel'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/relation/query_methods.rb:260:in `arel'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/relation/delegation.rb:29:in `respond_to?'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/association.rb:161:in `interpolate'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/association_scope.rb:8:in `interpolate'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/association_scope.rb:81:in `block (2 levels) in add_constraints'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/association_scope.rb:76:in `each'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/association_scope.rb:76:in `block in add_constraints'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/association_scope.rb:39:in `each'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/association_scope.rb:39:in `each_with_index'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/association_scope.rb:39:in `add_constraints'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/association_scope.rb:31:in `scope'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/association.rb:99:in `association_scope'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/association.rb:88:in `scoped'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/collection_association.rb:382:in `find_target'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/collection_association.rb:335:in `load_target'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.17/lib/active_record/associations/collection_proxy.rb:89:in `method_missing'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.17/lib/rails/commands/console.rb:47:in `start'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.17/lib/rails/commands/console.rb:8:in `start'
    from /Users/suryat/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.17/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in

我做错了什么?有什么建议吗?

1 个答案:

答案 0 :(得分:2)

设置关联如下:

has_and_belongs_to_many :managers, class_name: 'User', 
                        association_foreign_key: 'user_id', 
                        include: :roles, 
                        conditions: ['roles_users.role_id = ?', Role.select('id, name').find_by_name('manager').id]

您收到的错误为Association named 'roles' was not found on Team,因为您尝试在roles上调用roles_usersteams的关联(使用conditions选项)和{ {1}}与其中任何一个都没有关联。

在这种情况下,您需要使用teams选项指定二阶关联,以便在include表上应用roles的关联users