0

I'm not completely new to Rails, but I'm taking another pass at all the basic guides in an exploratory repo I've been using to flush out my own knowledge.

I'm on this one right now: http://guides.rubyonrails.org/association_basics.html

I got to section "3.5 Bi-Directional Associations".

I understood that model instances could be duplicated and out-of-sync when accessed in two-different ways in the same process which is something I want to ensure I learn how to avoid.

So I wrote a simple unit test to detect the problem in this commit: https://github.com/BadQuanta/simple-rails/commit/5e88ff08d5ba2743c4dbcaa0479e9adc8edc2a1f

The unit test seemed to work since it failed with the error I expected.

So I implemented the fix prescribed by the guide in this commit: https://github.com/BadQuanta/simple-rails/commit/7ac75b45730e3230162b8da280ba713004b8745c

Yet my unit test still would not pass and I could not figure out why.

So I did this to my unit test and I'm kinda stumped because this is exactly what I thought I wouldn't have to do with these relationships: https://github.com/BadQuanta/simple-rails/commit/7ac75b45730e3230162b8da280ba713004b8745c

Basically, what can I do to figure out what I'm doing wrong?

BadQuanta
  • 56
  • 5
  • What if you compare `c.object_id` and `o.customer.object_id`? – apneadiving Sep 01 '12 at 11:12
  • The object_ids differ. https://github.com/BadQuanta/simple-rails/commit/1fd47a4355dd4688bf6a993c1371721c70e2fd1a – BadQuanta Sep 01 '12 at 11:29
  • I know, and then you habe your answer. Unless you save/reload, the values would be different – apneadiving Sep 01 '12 at 11:33
  • So, I don't get the guide then because it seems to not give you an example of when you would use it. Since has_many and belongs_to are the majority of my relationships.. I'm just confused. I'm moving on for now and leaving it at that. – BadQuanta Sep 01 '12 at 11:36

1 Answers1

1

From the same guideline:

There are a few limitations to inverse_of support:

  • They do not work with :through associations.
  • They do not work with :polymorphic associations.
  • They do not work with :as associations.
  • For belongs_to associations, has_many inverse associations are ignored.

So why does the guide use them as EXAMPLES?

Community
  • 1
  • 1
BadQuanta
  • 56
  • 5