如何在两个模型之间建立多个 has_many 到 has_many 的关系?

How do I have multiple has_many to has_many relationship between two models?

我是 Rails 的新手,我对与另一个模型有多种关系的模型有疑问。这是我当前的设置,我可以通过 UserEvents 模型向用户添加事件。但是,显然我不能两次调用事件关系...这就是我难过的地方。

class User < ActiveRecord::Base
  has_many :user_events
  has_many :events, :through => :user_events
  has_many :events, :through => :user_participating
end

class UserEvents < ActiveRecord::Base
  belongs_to :event
  belongs_to :user
end

class UserParticipating < ActiveRecord::Base
  belongs_to :event
  belongs_to :user
end

class Events < ActiveRecord::Base
  has_one :user_event
  has_one :user, :through => :user_events
  has_many :user_participating
  has_many :user, :through => :user_participating
end

很有可能我做错了,但是,我已经花了好几个小时了,但似乎没有任何进展。所以,我想我会问。谢谢!

class User < ActiveRecord::Base
  has_many :user_events
  has_many :user_participatings
  has_many :events, through: :user_events
  has_many :participating_events, through: :user_participatings, class_name: 'Event'
end

class UserEvents < ActiveRecord::Base
  belongs_to :event
  belongs_to :user
end

class UserParticipating < ActiveRecord::Base
  belongs_to :event
  belongs_to :user
end

class Events < ActiveRecord::Base
  has_one :user_event
  has_one :user, through: :user_events
  has_many :user_participatings
  has_many :participants, through: :user_participatings, source: :user
end

即使我注意到一个事件 has_one 用户所以我不明白你为什么需要 UserEvent class