I am trying to create an association table using the “:through” option
in “has_many” but not having success. I have created the necessary
migrations of the association tables and run rake db:migrate
I have the following model:
class Group < ActiveRecord::Base
has_many :users, :through => :group_user
has_many :invites, :through => :group_invite, :class_name => “User”
belongs_to :owner, :class_name => “User”
end
class GroupUser < ActiveRecord::Base
belongs_to :group
belongs_to :user
end
Loading development environment (Rails 2.3.8)
g = Group.find(1)
=> #<Group id: 1, name: “My Group”, owner_id: 1, created_at: “2010-08-19
02:30:10”, updated_at: "
2010-08-19 02:30:10">g.users << User.find(2)
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the
association :group_user
in model Group
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/reflection.rb
:348:incheck_validity!' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/\ association_proxy.rb:56:in
initialize’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/
association_collection.rb:20:ininitialize' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations.\ rb:1306:in
new’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations.
rb:1306:in `users’
from (irb):2