Rails加入和多个关联

时间:2014-05-25 17:08:06

标签: sql ruby-on-rails ruby join left-join

我与另一个表有两个has_many关联:

has_many :sent_requests, foreign_key: :requester_id, class_name: 'FriendRequest'
has_many :pending_requests, foreign_key: :target_id, class_name: 'FriendRequest'

我想热切地加载这些关联,但includes没有帮助。

以下是来自日志的请求:

SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1

我接下来试过了:

joins("LEFT OUTER JOIN friend_requests as sent_requests 
       ON sent_requests.requester_id = users.id 
       LEFT OUTER JOIN friend_requests as pending_requests 
       ON pending_requests.target_id = users.id")

但这似乎并没有正确填补我的关联。我看到每个friend_request有多个用户。

如何加载我的关联?

0 个答案:

没有答案