In Rails 3.2.15, I'm trying to set up a has-many-through model called TopicInterest, to associate Users with Topics, with another attribute saved on TopicInterest called interest_type. After (I think) successfully setting up a join through a create() call to TopicInterest, I can't get the topic or user through the TopicInterest in the console:
irb(main):008:0> newjoin = TopicInterest.create(topic_id: mongo.id, user_id: me.id)
(0.4ms) BEGIN
SQL (0.6ms) INSERT INTO "topic_interests" ("created_at", "interest_type", "topic_id", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", Fri, 01 Aug 2014 16:10:00 UTC +00:00], ["interest_type", nil], ["topic_id", 1], ["updated_at", Fri, 01 Aug 2014 16:10:00 UTC +00:00], ["user_id", 1]]
(6.4ms) COMMIT
=> #<TopicInterest id: 2, user_id: 1, topic_id: 1, interest_type: nil, created_at: "2014-08-01 16:10:00", updated_at: "2014-08-01 16:10:00">
irb(main):009:0> newjoin.save
(0.3ms) BEGIN
(0.3ms) COMMIT
=> true
irb(main):010:0> newjoin.topic
NoMethodError: undefined method `topic' for #<TopicInterest:0x007fae2a06cc70>
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activemodel-3.2.15/lib/active_model/attribute_methods.rb:407:in `method_missing'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/attribute_methods.rb:149:in `method_missing'
from (irb):10
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/railties-3.2.15/lib/rails/commands/console.rb:47:in `start'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/railties-3.2.15/lib/rails/commands/console.rb:8:in `start'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/railties-3.2.15/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
User model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me
has_many :topics, through: :topic_interests
end
Topic model:
class Topic < ActiveRecord::Base
attr_accessible :name, :description
has_many :users, through: :topic_interests
end
TopicInterest model:
class TopicInterest < ActiveRecord::Base
attr_accessible :interest_type, :user_id, :topic_id
end
What am I doing wrong? Many thanks for any help.