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:
Account model defines an optional self-referential related_account association as follows:
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.
rubocop@0.52
introduced a newRails/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 aclass_name:
orforeign_key:
attribute) to explicitly specify the inverse relationship usinginverse_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:
Account
model defines an optional self-referentialrelated_account
association as follows:There is no inverse
has_one
association defined, so it is currently unclear if there should be one.Category
model has a parent/child association as follows:Adding
inverse_of: :children
andinverse_of: :parent
(respectively) caused aStack level too deep
error when trying to run the test suite.TransactionCategory
model has an association back to the baseTransaction
model as follows:But as only some transaction types are categorisable (e.g.
BasicTransaction
,Subtransaction
), addinginverse_of: :transaction_category
can't work as there's noTransaction.transaction_category
association.This may require using
polymorphic: true
andas:
, e.g.