rails has_many :through 或 has_many_belongs_to

rails has_many :through or has_many_belongs_to

我面临着组成联想的问题。

原来是

我有一个useruser可以有很多photo booths

photo booths可以有很多users.

这个问题已经解决了,但是我有Group photo booths.

Group photo booths 可以有很多 users 和很多 photo books。 所以

GroupPhotoBooth 有很多 UserPhotoBooth

PhotoBooth 有很多 User 和一个 GroupPhotoBooth

User 有很多 PhotoBoothsGroupPhotoBooths

class User < ApplicationRecord
  has_many :group_photo_booths
  has_many :photo_booths
end

class GroupPhotoBooth < ApplicationRecord
  has_many :photo_booth
  has_many :photo_booths
end

class PhotoBooth < AplicationRecord
  belongs_to :group_photo_booths
  has_many :users
end

但是这个顺序让我很困惑。我该怎么办?

如果您尝试在 class 之间建立关联,并且每个 class 都与用户相关。

那么你应该只创建 1 个 class,因为 User 和 User 有很多 'N' 关联。

这还不够吗?

class User < ApplicationRecord
  has_many :group_photo_booths
  has_many :photo_booths
end