active-hash/active_hash

Instance Variables and Re-used Objects

Opened this issue · 4 comments

I run into some trouble with ActiveHash when I set some per-request data on instance variables of an ActiveHash instance. I expected the ActiveHash finder methods to return fresh objects the way ActiveRecord does when you call find or where().first or whatnot.

I wrote a patch that separates the original state of the records in an ActiveHash sub-class from the records that are being passed around in the rest of the application. There is a bit of memory overhead to passing cloned objects instead of references to the same object, but that is true for ActiveRecord as well and it does not gum things up too much. I will submit a PR so you guys can have a look.

Alternatively to cloning objects, ActiveHash could freeze the records so they raise errors when developers try to set instance variables on them. This would give developers a better notion that the object they are using is not a clean/fresh copy like they'd expect it to be from ActiveRecord.

Ref #125

I agree that finder should returns fresh instance of ActiveHash whether setting instance variables or not.

Hi, issue still happen in v2.1.0, any plan on this one?

No progress yet.
If you are interested in this issue, PR is welcome!