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?

Reply
Join the discussion

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

Join 74,071+ 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. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.