0
私は_idで終わらず、非id主キーを指す外部キーで結合テーブルを使用しようとしています。ここに私が持っているものがあります。Rails 3 - _id列でないテーブルを結合する
私はテーブルを結合するので、次のようになります。ここでは
[DepatmentsLocales] (
department_id
locale_code
display_name
)
は私のモデルです:それでも
class Locale < ActiveRecord::Base
has_many :departments, :through => :departments_locales
end
class Department < ActiveRecord::Base
has_many :locales, :through => :departments_locales
end
class DepartmentLocale < ActiveRecord::Base
belongs_to :department
belongs_to :locale, :foreign_key => :locale_code, :primary_key => :code
end
、Railsが関連を見つけることができません。 department.localesを呼び出すと、私は次のようになります。
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :departments_locales in model Department
アイデアは何ですか?
入力いただきありがとうございます。機能的には動作しますが、私はそれをきれいに見つけることはできません。私は、 "ID"を結合テーブルのプライマリキーとして避け、department_localesをエンティティとして使用しないでください。私は私の場合、SQLを使用してフェッチすると思います。 –
DepartmentLocaleモデルテーブルのプライマリキーを、モデルのprimary_keyメソッドを使用して設定することができます。あなたが望むようにそれがすべて働くことを望みます!がんばろう –