简体   繁体   中英

How to build has_many :through relationship between the same Model (User) in Rails?

I want to build many-to-many relationship between the same Model (User).

I have User model:

class User < ActiveRecord::Base    
  has_many :broker_clients
  has_many :clients, :through => :broker_clients    
end

And BrokerClient:

class BrokerClients < ActiveRecord::Base
  belongs_to :broker, class_name: "User"
  belongs_to :client, class_name: "User"
end

When I use rails console and do something like:

>> User.first.clients
  User Load (0.7ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
NameError: uninitialized constant User::BrokerClient
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.1/lib/active_record/inheritance.rb:125:in `compute_type'
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.1/lib/active_record/reflection.rb:178:in `klass'
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.1/lib/active_record/reflection.rb:420:in `block in source_reflection'
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.1/lib/active_record/reflection.rb:420:in `collect'
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.1/lib/active_record/reflection.rb:420:in `source_reflection'
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.1/lib/active_record/reflection.rb:557:in `check_validity!'
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.1/lib/active_record/associations/association.rb:26:in `initialize'
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.1/lib/active_record/associations/has_many_through_association.rb:9:in `initialize'
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.1/lib/active_record/associations.rb:157:in `new'
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.1/lib/active_record/associations.rb:157:in `association'
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.1/lib/active_record/associations/builder/association.rb:70:in `clients'
    from (irb):4
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
    from /Users/info/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'>>

What I'm doing wrong here to get this error:

NameError: uninitialized constant User::BrokerClient

Try this:

class User < ActiveRecord::Base    
  has_many :broker_clients, :class_name => "User", :foreign_key => "broker_id"
  has_many :clients, :class_name => "User", :foreign_key => "client_id"

  #Your class definition here...
end

Access clients and broker_clients via:

   clients =  User.first.clients
   broker_clients = User.first.broker_clients 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM