在Rails中更改连接表中的列名称

时间:2013-06-28 19:17:25

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

我的协会:

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"

我是怎么做到的?

1 个答案:

答案 0 :(得分:1)

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

在woman.rb

scope :join_women_on_cell_id, lambda {
  joins('INNER JOIN "users" ON "users"."cellid" = "women"."cell_id"')
}