scottohara / loot

An implementation of some of the core MS Money features in Ruby on Rails
MIT License
4 stars 3 forks source link

Specify inverse_of on associations #127

Open scottohara opened 6 years ago

scottohara commented 6 years ago

rubocop@0.52 introduced a new Rails/InverseOf cop (enabled by default).

The cop requires all has_one/has_many <=> belongs_to associations where Rails can't automatically determine the inverse relationship (such as in the presence of a class_name: or foreign_key: attribute) to explicitly specify the inverse relationship using inverse_of:.

This caused a number of offences, and when trying to add the missing inverse_of: attributes, a number of problems were encountered (below), so this cop has been locally disabled for now.

If we can resolve these issues, the cop can be reenabled (preferred).

Issues:

There is no inverse has_one association defined, so it is currently unclear if there should be one.

Adding inverse_of: :children and inverse_of: :parent (respectively) caused a Stack level too deep error when trying to run the test suite.

But as only some transaction types are categorisable (e.g. BasicTransaction, Subtransaction), adding inverse_of: :transaction_category can't work as there's no Transaction.transaction_category association.

This may require using polymorphic: true and as:, e.g.

# basic_transaction.rb
has_one :transaction_category, foreign_key: 'transaction_id', dependent: :destroy, as: :categorisable_transaction

# transaction_category.rb 
belongs_to :categorisable_transaction, foreign_key: 'transaction_id', class_name: 'Transaction', polymorphic: true