3

I have a system where an account can have many different types of order ( SalesOrder, PurchaseOrder, SalesInvoice, PurchaseInvoice, etc)

I'm using single table inheritance and it works really well, even down to the level of controllers and views.

But how do I declare inverse_of on the associations.

In my account it's easy on each declaration

has_many sales_orders, inverse_of: :account
has_many purchase_orders, inverse_of: account
...

But on the base model for all the types of orders I have"

belongs_to :account

I want to add inverse_of: xyz where xyz is worked out dynamically according to the model. For example, for a SaleOrder in would be inverse_of: :sales_orders.

But I want to keep the code in the base model. I do not want to go into the SalesOrder and write belongs_to :account, inverse_of: :sales_orders there.

How can I do it? Can I use a lambda on the association declaration that returns the right value according to the sub-class being used?

SRack
  • 11,495
  • 5
  • 47
  • 60
John Small
  • 942
  • 2
  • 12
  • 21

0 Answers0