watzon
watzon

Reputation: 2549

one model belongs to two models

I know similar questions have been asked before, but can't find one that's close enough to what I need for me to learn how to do this.

I have 3 models: wedding, reception, and attendee

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.

What I am having the most trouble with is wrapping my head around the idea of having one model belong to two other models. Namely the reception model belonging to both the wedding model and the attendee model.

How can I go about doing this?

This is my schema:

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"

Oh, I also have an event's table. I am using the ActiveRecord::ActsAs gem to make weddings act as Events.

Upvotes: 0

Views: 1012

Answers (2)

joshua.paling
joshua.paling

Reputation: 13952

The Reception model definitely doesn't belong_to the Attendee model.

The relationship between Receptions and Attendees will be either a has_many, or a 'has_many through' relationship. It's worth reading through the full doco on assocations.

You mention:

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.

Just to clarify we're on the right page, is it true that a single wedding will have multiple receptions? And that an individual guest can select which of those multiple receptions they're going to attend? So a guest may attend 2 out of 3 receptions, for a single wedding? And what about the wedding itself? Can guests attend some of the receptions but not the related wedding? Or if they're attending any of the receptions, is it automatically assumed they'll be attending the wedding, too?

And how do you determine if a particular person is in fact invited to a wedding? Can I browse your site and just list myself as an attendee for several receptions, uninvited?

Something seems off in the way you've described the data model - at least, the data structure you've described doesn't match weddings we're all used to. You may well have a valid reason for this. If so, update your question with a bit more background info on the underlying problem you're trying to solve, and the real-life situations you're trying to model. It'll allow people to help you more easily.

Update: this 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 a "has many through" relationship between attendees and receptions. That'll mean that you'll need a new model and database table, reception_attendees, which will be a links table between receptions and attendees.

Upvotes: 1

pauloancheta
pauloancheta

Reputation: 359

I think these relationship works:

# wedding.rb
has_many :attendees
has_one :reception

# attendee.rb
belongs_to :wedding
belongs_to :reception

# reception.rb
belongs_to :wedding
has_many :attendees

Upvotes: 0

Related Questions