【发布时间】:2015-10-18 21:07:00
【问题描述】:
模型
class Feature < ActiveRecord::Base
belongs_to :agency_feature
...
end
class Agency < ActiveRecord::Base
has_many :agency_features, dependent: :destroy
...
end
class AgencyFeature < ActiveRecord::Base
belongs_to :agency
has_one :feature
end
架构
create_table "agency_features", force: true do |t|
t.integer "agency_id"
t.integer "feature_id"
t.boolean "enabled"
end
add_index "agency_features", ["agency_id"], name: "index_agency_features_on_agency_id", using: :btree
add_index "agency_features", ["feature_id"], name: "index_agency_features_on_feature_id", using: :btree
问题
Agency.first.agency_feature 给我:
<AgencyFeature id: 508, agency_id: 1, feature_id: 1, enabled: false>
和Agency.first.agency_features.first.agency返回正确的代理。
问题是Agency.first.agency_features.first.feature 给出了一个列不存在 错误,并试图在特征内部寻找"agency_feature_id"。
如何让它查找 ID 与 AgencyFeature 内的“feature_id”属性相对应的功能?
【问题讨论】:
标签: ruby-on-rails ruby activerecord rails-activerecord rails-migrations