Reputation: 1604
I am trying to learn to model SQL databases, and am having some trouble understanding some concepts. I want to a build an app, where users can split a bill at a restaurant. Could someone please tell me if the following is allowed?
I have Bills, Items, and Users
Bill has_many :items, dependent: :destroy
Bill has_many :users, foreign_key: :user_id
User has_many :bills, foreign_key: :created_by
Item belongs_to :bill
Is it ok for a user to have many bills, and a bill to have many users?
Upvotes: 0
Views: 1105
Reputation: 600
This is a more fundamental database design issue. If I were to design this database for a bill sharing app, it would be like this
user.rb
has_many :bill_contributions
has_many :bills, through: :bill_contributions
bill.rb
has_many :items
has_many :bill_contributions
has_many :users, through: :bill_contributions
bill_contribution.rb
belongs_to :user
belongs_to :bill
I split it like this because there is a has many and belongs to many relationship between the user and bills as a user can have a lot of bills and bills can be split among many users. you can look up HABTM database relationships if you wish to learn more
Upvotes: 1