has_many通过与同一模型的关系

时间:2014-10-10 18:05:05

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

我有用户表:

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

和BrokerClients表:

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

现在我创建了一段关系:

>> BrokerClients.create(broker_id: User.first.id, client_id: User.last.id)
  User Load (9.7ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
  User Load (1.3ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
   (1.3ms)  BEGIN
  SQL (41.5ms)  INSERT INTO "broker_clients" ("broker_id", "client_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["broker_id", 4], ["client_id", 210], ["created_at", Fri, 10 Oct 2014 13:43:27 EDT -04:00], ["updated_at", Fri, 10 Oct 2014 13:43:27 EDT -04:00]]
   (0.5ms)  COMMIT
=> #<BrokerClients id: 1, broker_id: 4, client_id: 210, created_at: "2014-10-10 17:43:27", updated_at: "2014-10-10 17:43:27">
>> User.first.brokers.first

当我试图让客户正常工作时:

>> User.first.clients.first
  User Load (0.6ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
  User Load (1.2ms)  SELECT "users".* FROM "users" INNER JOIN "broker_clients" ON "users"."id" = "broker_clients"."
=> #<User id: 210, ....

但对于客户来说,当我试图让与之相关的经纪人无法工作时:

>> User.last.brokers.first
  User Load (0.7ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
  User Load (0.7ms)  SELECT "users".* FROM "users" INNER JOIN "broker_clients" ON "users"."id" = "broker_clients"."broker_id" WHERE "broker_clients"."broker_id" = $1 ORDER BY "users"."id" ASC LIMIT 1  [["broker_id", 210]]
=> nil

有任何帮助吗?

2 个答案:

答案 0 :(得分:1)

User modal更改为: -

has_many :broker_clients, :class_name => "BrokerClients", :foreign_key => "broker_id"
has_many :clients, :through => :broker_clients, :foreign_key => "broker_id"

has_many :inverse_broker_clients, :class_name => "BrokerClients", :foreign_key => "client_id"
has_many :brokers, :through => :inverse_broker_clients, :foreign_key => "client_id"

答案 1 :(得分:0)

查看您的插入内容与您的查询:

插入:

  

SQL(41.5ms)INSERT INTO“broker_clients”(“broker_id”,“client_id”,“created_at”,“updated_at”)VALUES($ 1,$ 2,$ 3,$ 4)返回“id”[[“broker_id”, 4],[“client_id”,210],[“created_at”,星期五,2014年10月10日13:43:27 EDT -04:00],[“updated_at”,星期五,2014年10月10日13:43:27美国东部时间 - 04:00]

查询:

  

用户负载(0.7ms)SELECT“users”。* FROM“users”INNER JOIN“broker_clients”ON“users”。“id”=“broker_clients”。“broker_id”WHERE“broker_clients”。“broker_id”= $ 1 ORDER BY“users”。“id”ASC LIMIT 1 [[“broker_id”,210]]

您插入(broker_id = 4,client_id = 210)但查询broker_id = 210。这就是你为brokers.first获得nil的原因。很确定你的外键是倒退的:

has_many :clients, :through => :broker_clients, :foreign_key => "broker_id"
has_many :brokers, :through => :broker_clients, :foreign_key => "client_id"

应该是

has_many :clients, :through => :broker_clients, :foreign_key => "client_id"
has_many :brokers, :through => :broker_clients, :foreign_key => "broker_id"

希望有所帮助。