Rails 3 - belongs_to user AND has_many users



我正试图弄清楚如何执行以下操作:

一场属于用户(创作者)的音乐会,有许多嘉宾和组织者。

以下方法好吗?

音乐会:

 class Concert < ActiveRecord::Base
  belongs_to :user
  has_many :guests, :class_name => 'User'
  has_many :organisers, :class_name => 'User'
 end

用户:

 class User < ActiveRecord::Base
  has_many :concerts
 end

提前感谢,

Ad两个新模型来保持has_many关系:

 class Concert < ActiveRecord::Base
  belongs_to :user
  has_many   :concert_guests
  has_many   :concert_organizers
  has_many   :guests, :through => :concert_guests, :source => :user
  has_many   :organizers, :through => :concert_organizers, :source => :user
 end
 class User < ActiveRecord::Base
  has_many :concerts
 end
 # table with user_id and concert_id columns   
 class ConcertGuest
   belongs_to :user
   belongs_to :concert
 end
 # table with user_id and concert_id columns   
 class ConcertOrganizer
   belongs_to :user
   belongs_to :concert
 end

相关内容

  • 没有找到相关文章

最新更新