我的Rails 3多对多关联出了什么问题?

时间:2011-10-13 03:55:32

标签: ruby-on-rails ruby-on-rails-3 activerecord

我无法弄清楚我在这里做错了什么..我似乎无法在“has_many:creators”行中获得Event类...我创建了一个rspec测试来验证一个事件实例化将响应'创造者',但我不能让它通过......任何想法赞赏!

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

1 个答案:

答案 0 :(得分:2)

EventInvitation belongs_to'user',但您正在存储'creator_id'。您需要存储'user_id',或者调用关联'创建者'。

belongs_to :creator, :class_name => 'User'

在您的活动模型中,您可以使用:

has_many :creators, :through => :event_invitations