ruby on rails - one model belongs to two models -
i know similar questions have been asked before, can't find 1 that's close enough need me learn how this.
i have 3 models: wedding, reception, , attendee
the idea user able go wedding's event page have info, address, , list of receptions. user able select receptions they're going at, insert name , address, , inserted database attendee wedding.
what having trouble wrapping head around idea of having 1 model belong 2 other models. namely reception model belonging both wedding model , attendee model.
how can go doing this?
this schema:
create_table "attendees", force: :cascade |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 |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 |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 |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"
oh, have event's table. using activerecord::actsas gem make weddings act events.
the reception
model doesn't belong_to
attendee
model.
the relationship between receptions , attendees either has_many, or 'has_many through' relationship. it's worth reading through full doco on assocations.
you mention:
the idea user able go wedding's event page have info, address, , list of receptions. user able select receptions they're going at, insert name , address, , inserted database attendee wedding.
just clarify we're on right page, true single wedding have multiple receptions? , individual guest can select of multiple receptions they're going attend? guest may attend 2 out of 3 receptions, single wedding? , wedding itself? can guests attend of receptions not related wedding? or if they're attending of receptions, automatically assumed they'll attending wedding, too?
and how determine if particular person in fact invited wedding? can browse site , list myself attendee several receptions, uninvited?
something seems off in way you've described data model - @ least, data structure you've described doesn't match weddings we're used to. may have valid reason this. if so, update question bit more background info on underlying problem you're trying solve, , real-life situations you're trying model. it'll allow people more easily.
update: should work
# 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
so you'll need "has many through" relationship between attendees , receptions. that'll mean you'll need new model , database table, reception_attendees
, links table between receptions , attendees.
Comments
Post a Comment