Inc1982
Inc1982

Reputation: 1975

What's wrong with my Rails 3 Many-to-Many Associaton?

I can't figure out what I"m doing wrong here.. I can't seem to get the Event class correct in regards to the "has_many :creators" line... I created an rspec test to verify that an event instantiation will respond to 'creators' but I can't get it to pass... Any thoughts appreciated!

class Event < ActiveRecord::Base
has_many    :event_invitations
has_many    :creators,   :through => :event_invitations,
                 :source => :creator,
                 :class_name => "User"

class EventInvitation < ActiveRecord::Base
belongs_to  :user
belongs_to  :event

class User < ActiveRecord::Base

  has_many  :event_invitations,   :foreign_key => :creator_id
  has_many  :created_events,      :through => :event_invitations,
                                  :source => :event

Upvotes: 2

Views: 49

Answers (1)

bricker
bricker

Reputation: 8941

EventInvitation belongs_to 'user', but you're storing 'creator_id'. You need to either store 'user_id', or call the association 'creator'.

belongs_to :creator, :class_name => 'User'

And in your Event model, you can use this:

has_many :creators, :through => :event_invitations

Upvotes: 2

Related Questions