gpt4 book ai didi

ruby-on-rails - Rails 3 - belongs_to user AND has_many users

转载 作者:搜寻专家 更新时间:2023-10-30 23:21:21 24 4
gpt4 key购买 nike

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

一场音乐会属于一个用户(创作者),有_很多客人和有_很多组织者。

下面的方法好吗?

音乐会:

 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

提前致谢

最佳答案

添加两个新模型来保存 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

关于ruby-on-rails - Rails 3 - belongs_to user AND has_many users,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6259015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com