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:
- pack_id
- profile_id (FK)
- beer_id (FK1)
- beer_id (FK2)
- beer_ip (FK3)
- 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:
- pack_id
- profile_id (FK)
- beer_id (FK)
- 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):
- pack_id
- profile_id (FK)
- beers [beer_id1 , beer_id2, beer_id3] # or hash [beer_1: beer_id, beer_2:..]
- 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
- rating_id
- User_id (FK)
- beer_id (FK)
- rate
- 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!!