7

I am developing a web app using rails 4 for the first time. I am making all of my model associations bidirectional and using inverse_of wherever it is allowed.

From reading the documentation, I've developed the impression that this is probably the best practice, but that's never really spelled out clearly anywhere.

I'd appreciate any general advice in this regard from experienced rails developers. I hope the question is not too vague to have value here.

Thanks!

Update: In addition to non-standard names, there appear to be two main additional cases where explicitly setting inverse_of is needed:

  1. for INVALID_AUTOMATIC_INVERSE_OPTIONS ( http://www.rubydoc.info/docs/rails/ActiveRecord/Reflection/AssociationReflection )
  2. if you're accepting nested attributes - This is because the parent object is not saved yet and thus does not have an id. See http://viget.com/extend/exploring-the-inverse-of-option-on-rails-model-associations
Nested Software
  • 303
  • 4
  • 15

1 Answers1

4

It is typically desirable to have inverses set up. The good news is that this is handled automatically in most cases. You usually only need to explicitly set the inverses for associations with non-standard names. You can find out a lot more in the official documentation.

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
  • Thanks. I find that I still ran into the problem described here: http://viget.com/extend/exploring-the-inverse-of-option-on-rails-model-associations under the heading _Creating an object and its children via accepts_nested_attributes_for in a :has_many association._ I find it's easier to just set the inverses for every association and that way I don't have to worry about whether or not it's going to "just work" for a given case... – Nested Software Aug 12 '15 at 22:37
  • 4
    @Nesteddoll Easier in the short term, perhaps. I manage literally hundreds of production Rails applications and I can tell you with certainty that littering your application with unnecessary code that you don't understand will quickly become a maintenance nightmare. Challenge yourself to be better, don't add extra code to your app that you don't need and don't add code that you don't understand. Your future self will thank you. – Brad Werth Aug 13 '15 at 07:11
  • thank you. I do appreciate what you're saying - and I will look into inverse_of in more detail. – Nested Software Aug 13 '15 at 22:38
  • 1
    Hi Brad, I'll accept your answer - I needed to look up how to do that as I am a new so user :). In addition to non-standard names, I there appear to be two main additional cases where explicitly setting inverse_of is needed: 1. for invalid_automatic_inverse_options ( http://www.rubydoc.info/docs/rails/ActiveRecord/Reflection/AssociationReflection ) 2. if you're accepting nested attributes as per ( http://viget.com/extend/exploring-the-inverse-of-option-on-rails-model-associations ) Thank you for holding me to account. It definitely cleaned up the code when I was able to remove the extra ones – Nested Software Aug 13 '15 at 23:45