0

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:

  1. cutting index:true from the belongs_to association
  2. destroy and recreating the DB (about 25 times)
  3. 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)>'
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
nialbima
  • 11
  • 3

3 Answers3

0

Your resource_topics table does not have the resource_id column it needs to have in order for this association to work.

Your migration file does not necessarily represent the actual state of the database. You have either not run the migration yet, or you ran an earlier version of the migration which did not introduce the resource_id column, and then modified the migration file without re-running it.

user229044
  • 232,980
  • 40
  • 330
  • 338
0

DB:RESET - reads from the schema.

DB:ROLLBACK - reads from the latest migration. In this case, even after migrating, using db:reset meant errors i'd fixed in the other files persisted.

nialbima
  • 11
  • 3
0

I know this is a bit old, but in your question you say it will

"refuse to save the resource_id"

but you have declared :resource_id as an attr_accessor, which means it doesn't have a db column and isn't persisted to the database. Look here for clarification. There is a great one-line summary in the answer from @Kaleidoscope when he says

"If you declare an attr_accessor then you can use it as a virtual attribute, which is basically an attribute on the model that isn't persisted to the database."

It's not clear to me, though, how you are using :resource_id. I've just run into this same error, not in trying to persist a value to the database, but simply trying to assign the attribute a value. If your solution was different than what I've provided, I would love to know what it was in the event it is a potential solution to my error.

Community
  • 1
  • 1
eggroll
  • 1,049
  • 9
  • 18