I'm building a Rails project with PG, and trying to use the ResourceTopic table to link my Topics and Resources together (so I can assign multiple tags to one resource). I'm also at my wit's end! It'll save the topic_id, and then refuse to save the resource_id.
I have this line at the bottom of my db:seeds file:
ResourceTopic.create({topic: Topic.find(1), resource: Resource.find(1)})
My models:
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
Here's the migration in question:
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
Things I've tried:
- cutting index:true from the belongs_to association
- destroy and recreating the DB (about 25 times)
- reordering the associations to match a working example
Literal error (line 66 is what I copied at the top of the file):
ActiveModel::MissingAttributeError: can't write unknown attribute `resource_id`
/Users/galactus/code/panic_button/db/seeds.rb:66:in `<top (required)>'