2011-06-06 10 views
1

Railsの3 - ユーザとhas_manyのユーザーbelongs_toの私は次の操作を行う方法を把握しようとしています

ユーザー(作成者)、has_manyのゲストとhas_manyの主催belongs_toのコンサート。

次のアプローチは良いですか?

コンサート:

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 

おかげで、

+0

。あなたは何の問題を抱えていますか? – ipd

+0

ここに素敵なリソース:http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through – apneadiving

答えて

2

広告にhas_manyの関係を保持するための2つの新しいモデル:

正常に見える
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 
+0

これは最も簡単な方法ですか?少し複雑に思えます:) – invaino

+0

'User'と' Concert'モデルの間にhas_many関係を維持したい場合、これはこれを行う方法の1つです。 –

+0

確かに:)ありがとう! – invaino

関連する問題