1

I'm not sure how to model the following requirements:

We send packs of 3 different beers to our users. The users are divided in 4 categories of tasting_profiles. Then, we prepare 4 different packs of beers for all our users. Then the users rate the beers and this will happen in a periodic basis.

NOTE: My main concern is that I have a table (Packs) that may have multiple instances of the same foreign key. Every pack consists of 3 kinds of beers.

Currently I have the following models:

User / Beer / Profile / Pack / rating

class User < ActiveRecord::Base
  belongs_to :profile 
  has_many :ratings # A user rates every beer received.
  has_many :beers, through: :ratings
end

class Beer < ActiveRecord::Base
  has_many :ratings
  has_many :users, through: :ratings
  has_many :packs
end

class Profile < ActiveRecord::Base
  has_many :packs #we setup and send periodic packs for each tasting_profile (Profile)
  has_many :users #each tasting profile has many users
  has_may :beers #each tasting profile has many possible beers
end

class Pack < ActiveRecord::Base
  belongs_to :beer #Not sure
  belongs_to :profile
end

class Rating < ActiveRecord::Base
  belongs_to :user
  belongs_to :beer
end

The pack model question: My first option was to have the following fields in the Pack model:

  1. pack_id
  2. profile_id (FK)
  3. beer_id (FK1)
  4. beer_id (FK2)
  5. beer_ip (FK3)
  6. delivery_month

This way I have one complete pack for entry.

Researching here I read that apparently this is a bad practice and only one FK for entrance was suggested. Something like this:

  1. pack_id
  2. profile_id (FK)
  3. beer_id (FK)
  4. delivery_month

In this case, i would have 3 entries for each pack.

Finally I was considering to study an array field (not sure if it can be done with Foreign keys):

  1. pack_id
  2. profile_id (FK)
  3. beers [beer_id1 , beer_id2, beer_id3] # or hash [beer_1: beer_id, beer_2:..]
  4. delivery_month

After a pack is created I need to populate the rating table with as many of the following entries as needed according to the number of users. If I have 100 users in the tasting_profile, I will send the 3 beers pack to them, resulting in 300 entries here

  1. rating_id
  2. User_id (FK)
  3. beer_id (FK)
  4. rate
  5. delivery_date

I'm really confused about how to properly model this. ¡And I would thank any possible help! I tried to be as detailed as possible. Please let me know if you need further clarification.

¡¡Thanks in advance!!

Lastimoso
  • 218
  • 3
  • 11

1 Answers1

0

It took me a lot of time but was simpler than expected. As soon as I needed more than one instance of the same foreign_id in the same table it should have been clear that I needed yet another join table.

First I tried with a has_and_belongs_to_many association but I had problems deleting an "association" entry in the join table. So I ended with a has_many Trough. These are the changes:

class Beer < ActiveRecord::Base
  has_many :pack_details
  has_many :packs through: :pack_details
  ...
end

class Pack < ActiveRecord::Base
  has_many :pack_details, dependent: destroy
  has_many _beers, through: : pack_details
  accepts_nested_attributes_for :pack_details, allow_destroy => true
  ...
end

class PackDetail < ActiveRecord::Base
  Belongs_to :pack
  Belongs_to :beer
  ...
end

Everything is working properly!

Lastimoso
  • 218
  • 3
  • 11