Question

I have a rather odd situation here.

My OrdersController has an edit method and at some point I have the following statement:

order_item.item = item

As you can guess, order_item.item expects a reference to an Item object, which is exactly what item is. Here comes the mistery. The first time I send a request to orders/{some_order_id}/edit, I receive the expected response. But if I send a new request (even if I just press F5) the application crashes and I receive the following error message:

ActiveRecord::AssociationTypeMismatch in OrdersController#edit

Item(#177601092) expected, got Item(#67520280)

What have I discovered so far? I have put an puts "#{Item.object_id}" just before the order_item.item = item and found out that Item.object_id changes from a request to another, which means that the Item ActiveRecord is changing its position on memory. (For instance, in the above example, during the first request Item.object_id is 67520280, while in the second it changes to 177601092.)

My question is: What can be causing the Item ActiveRecord to change its position on memory?

Useful info: I'm using rails 3.0.20

Update: I found out that this change of ID is pretty common. But I still can't figure out why the expectation doesn't change as the Item.object_id does.

Was it helpful?

Solution

Despite the fact that I am not using Factory Girl, this answer and this discussion helped me a lot.

tl;dr

The problem was solved when I set cached_classes to true in my environment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top