我正在用PG構建Rails項目,並試圖使用ResourceTopic表將我的主題和資源鏈接在一起(這樣我可以將多個標籤分配給一個資源)。我也在智慧的結尾!它會保存topic_id,然後拒絕保存resource_id。未知屬性錯誤 - 無法寫入resource_id
我有這條線在我的數據庫的底部:種子文件:
ResourceTopic.create({topic: Topic.find(1), resource: Resource.find(1)})
我的模型:
class Resource < ActiveRecord::Base
attr_accessor :resource_id
belongs_to :user
has_many :resource_topics
has_many :topics, :through => :resource_topics
end
class Topic < ActiveRecord::Base
has_many :resource_topics
has_many :resources, :through => :resource_topics
end
class ResourceTopic < ActiveRecord::Base
belongs_to :topic
belongs_to :resource
end
這裏是有問題的遷移:
class CreateResourceTopics < ActiveRecord::Migration
def change
create_table :resource_topics do |t|
t.belongs_to :resource, index:true, foreign_key: true
t.belongs_to :topic, index:true, foreign_key: true
t.timestamps null: false
end
end
end
事情我已經嘗試過:
- 切割指數:從belongs_to的關聯
- 破壞並重新創建DB真(約25倍)
- 重新排序協會
文字錯誤匹配工作示例(第66行是我抄在文件的頂部):
ActiveModel::MissingAttributeError: can't write unknown attribute `resource_id`
/Users/galactus/code/panic_button/db/seeds.rb:66:in `<top (required)>'