一个模型属于两个模型

one model belongs to two models

我知道以前有人问过类似的问题,但找不到足够接近我学习如何做这件事所需的问题。

我有 3 个模型:婚礼、招待会和与会者

我们的想法是,用户将能够转到婚礼的活动页面,其中包含一些信息、地址和招待会列表。用户将能够 select 他们将要参加的招待会,插入他们的姓名和地址,然后作为该婚礼的参加者被插入到数据库中。

让我最头疼的是让一个模型属于另外两个模型的想法。即reception模型既属于wedding模型又属于attende模型。

我该怎么做?

这是我的架构:

create_table "attendees", force: :cascade do |t|
    t.string   "name",       limit: 255
    t.string   "address",    limit: 255
    t.integer  "people",     limit: 4
    t.string   "receptions", limit: 255
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
end
create_table "events", force: :cascade do |t|
    t.string   "name",         limit: 255
    t.string   "slug",         limit: 255
    t.text     "description",  limit: 65535
    t.string   "address",      limit: 255
    t.float    "latitude",     limit: 24
    t.float    "longitude",    limit: 24
    t.datetime "start_time"
    t.datetime "end_time"
    t.boolean  "saved",        limit: 1,     default: false
    t.integer  "actable_id",   limit: 4
    t.string   "actable_type", limit: 255
    t.datetime "created_at",                                 null: false
    t.datetime "updated_at",                                 null: false
  end

  create_table "receptions", force: :cascade do |t|
    t.string   "address",    limit: 255
    t.float    "latitude",   limit: 24
    t.float    "longitude",  limit: 24
    t.integer  "wedding_id", limit: 4
    t.datetime "start_time"
    t.datetime "end_time"
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
  end

add_index "receptions", ["wedding_id"], name: "fk_rails_40f4f8c049", using: :btree

create_table "weddings", force: :cascade do |t|
    t.string   "brides_name", limit: 255
    t.string   "grooms_name", limit: 255
    t.datetime "created_at",              null: false
    t.datetime "updated_at",              null: false
  end

  add_foreign_key "receptions", "weddings"

哦,我还有一个活动的table。我正在使用 ActiveRecord::ActsAs gem 让婚礼成为活动。

Reception 模型绝对不是 belong_to Attendee 模型。

招待会和与会者之间的关系将是 has_many 或 'has_many through' 关系。值得通读 full doco 上的关联。

你提到:

The idea is that a user will be able to go to the wedding's event page where it will have some info, an address, and a list of receptions. The user will be able to select what receptions they're going to be at, insert their name and address, and then be inserted into the database as an attendee for that wedding.

澄清一下,我们在正确的页面上,一场婚礼真的会有多次招待会吗?并且单个客人可以 select 他们将参加这些多个招待会中的哪一个?那么对于一场婚礼,一位宾客可以参加 3 场招待会中的 2 场?婚礼本身呢?客人可以参加一些招待会,但不能参加相关的婚礼吗?或者,如果他们参加任何招待会,是否会自动假定他们也将参加婚礼?

你如何确定某个人是否真的被邀请参加婚礼?我可以浏览您的网站并在不请自来的情况下将自己列为几个招待会的与会者吗?

您描述数据模型的方式似乎有些不对劲 - 至少,您描述的数据结构与我们都习惯的婚礼不符。您可能对此有充分的理由。如果是这样,请使用更多有关您要解决的潜在问题以及您要建模的现实情况的背景信息来更新您的问题。它会让人们更容易地帮助你。

更新:这应该有效

# wedding.rb
has_many :receptions

# reception.rb
belongs_to :wedding
has_many :attendees, through: :reception_attendees

# attendee.rb
belongs_to :wedding
has_many :receptions, through: :reception_attendees

# reception_attendee.rb
belongs_to :reception
belongs_to :attendee

因此您需要在与会者和招待会之间建立 "has many through" 关系。这意味着您将需要一个新模型和数据库 table、reception_attendees,这将是招待会和与会者之间的链接 table。

我认为这些关系有效:

# wedding.rb
has_many :attendees
has_one :reception

# attendee.rb
belongs_to :wedding
belongs_to :reception

# reception.rb
belongs_to :wedding
has_many :attendees