Ruby on Rails
Sunday, September 21, 2014
Assuming a single creditor and multiple debtors for a single billing, you will need a one to one relationship for the creditor -> billing (belongs_to on the billing, as you have done), but a many to many relationship for debts -> users.
Generally, it's better to implement the many-to-many as a has_many :through so you can add logic. It's probably doubly important when there is money involved!
I would rethink the dependent: destroy though. You probably want to use a soft delete instead, or else you will lose your past transactions.
On 21 September 2014 05:37, Diego Dillenburg Bueno <diegodillenburg@gmail.com> wrote:
Hello everyone,--I'm a beginner Rails developer and right now am building a sample project to show off at some job apply. The app is rather simple, I guess, but I have come to some doubts on what associations to chose and why.Basically a User can create a bill(being its creditor) and the billing can have many other Users(as their debtors), so you can track up billings you share with other persons, like rent, market, food orders etc, and control what each one got to pay you.Right now I have the following model:class User < ActiveRecord::Basehas_many :billings, foreign_key: "creditor_id", dependent: :destroyhas_many :debts, foreign_key: "debtor_id", dependent: :destroyendclass Billing < ActiveRecord::Basebelongs_to :creditor, class_name: "User"has_many :debts, dependent: :destroyendclass Debt < ActiveRecord::Basebelongs_to :billingbelongs_to :userendwould this be the better approach? Or something like:User has_many billings through debtsBilling has_many debts ?And in that case, would appreciate some help to model those associations, because I'm still kinda lost on this flow.Thanks in advance,Diego Dillenburg Bueno
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAOHSkmE893vhkwHsna6e5Ax4fCZipg965brtieXtr9Ui6OAVdw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CABLJ1D%2BrD9ENPDHS5xAAH_aYr2f33uq%2BMQRkmxPn%2B6-i%3DY_Q%3Dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment