Ask A Question


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
Create an account Log in

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.