Save 36% for Black Friday! Learn more

All threads / Is it possible to make multiple belongs_to or has_one association?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Is it possible to make multiple belongs_to or has_one association?

Kolbasoid asked in Rails

I have a table Operations with two columns referenced to Accounts. One column is called debit_id and other credit_id. Model looks like

    belongs_to :debit,  class_name: :Account, optional: true
    belongs_to :credit, class_name: :Account, optional: true

So I want to include reference to Ledger model that is referenced in Accounts model from Operations. I don't have direct account_id in my Operations I need some workaraound as far as it's impossible to do smth like that

has_one :ledger, through: :debit
has_one :ledger, through: :credit

Is it possible at all in Rails?

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 39,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.