模型
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
给出了一个column doesn't exist 错误并且正在尝试寻找 "agency_feature_id"
功能内部。
如何让它查找 ID 与 AgencyFeature 中的“feature_id”属性对应的特征?
最佳答案
也许再次尝试运行迁移。我同意 Marnuss 的观点。我认为您的功能表中没有字段 agency_feature_id 。它应该看起来像 -
create_table "features", force: true do |t|
t.integer "agency_feature_id"
...
end
关于ruby-on-rails - rails/事件记录 : not retrieving the correct column through a join with a has_one/belongs_to relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33203260/