【发布时间】:2014-05-25 21:20:39
【问题描述】:
我有一个使用belongs_to 关系的模型。我希望能够同时指定 foreign_key 和 association_foreign_key 值。但是,我只能为belongs_to 关系(http://guides.rubyonrails.org/association_basics.html#belongs-to-association-reference)指定foreign_key 值。有没有办法解决这个问题?
这是我的例子:
我有一个客户端模型。它的 location_id 键必须属于 Region 模型,其中 id 由 place_id 引用。我想做的是:
class ClientId < ActiveRecord::Base
belongs_to :region, foreign_key: 'location_id', association_foreign_key: 'place_id'
但是,我无法在此处指定 association_foreign_key...
【问题讨论】:
-
你没有解释清楚。你是说
Region的主键是place_id?在这种情况下你不需要association_foreign_key,因为它不是has_and_belongs_to_many。
标签: ruby-on-rails activerecord associations belongs-to