1

我的协会:

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"

我该怎么做?

4

1 回答 1

2

大多数人会在原始 SQL 中编写连接并将其封装在一个范围内:

在女人.rb

scope :join_women_on_cell_id, lambda {
  joins('INNER JOIN "users" ON "users"."cellid" = "women"."cell_id"')
}
于 2013-06-28T19:23:38.243 回答