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 <
diegodillenb...@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::Base
> has_many :billings, foreign_key: "creditor_id", dependent: :destroy
> has_many :debts, foreign_key: "debtor_id", dependent: :destroy
> end
>
> class Billing < ActiveRecord::Base
> belongs_to :creditor, class_name: "User"
> has_many :debts, dependent: :destroy
> end
>
> class Debt < ActiveRecord::Base
> belongs_to :billing
> belongs_to :user
> end
>
> would this be the better approach? Or something like:
> User has_many billings through debts
> Billing 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+unsubscr...@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
> <https://groups.google.com/d/msgid/rubyonrails-talk/CAOHSkmE893vhkwHsna6e5Ax4fCZipg965brtieXtr9Ui6OAVdw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> 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+unsubscr...@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.

Reply via email to