我的协会:
class User < ActiveRecord::Base
has_many :women, foreign_key: "cell_id"
end
class Woman < ActiveRecord::Base
belongs_to :users, foreign_key: "cellid"
end
这是我的加入
@all = Woman.joins(:users)
我有选择
SELECT "women".* FROM "women" INNER JOIN "users" ON "users"."cellid" = "women"."id"
但我需要
SELECT "women".* FROM "women" INNER JOIN "users" ON "users"."cellid" = "women"."cell_id"
我是怎么做到的?
答案 0 :(得分:1)
大多数人会在原始SQL中编写连接并将其封装在范围内:
在woman.rb
scope :join_women_on_cell_id, lambda {
joins('INNER JOIN "users" ON "users"."cellid" = "women"."cell_id"')
}