Preventing ActiveRecord save() on an instance

Posted by Craig Walker on Stack Overflow See other posts from Stack Overflow or by Craig Walker
Published on 2010-05-30T00:00:53Z Indexed on 2010/05/30 0:02 UTC
Read the original article Hit count: 240

Filed under:
|
|
|

I have an ActiveRecord model object Foo; it represents a standard database row.

I want to be able to display modified versions of instances of this object. I'd like to reuse the class itself, as it already has all the hooks & aspects I'll need. (For example: I already have a view that displays the appropriate attributes). Basically I want to clone the model instance, modify some of its properties, and feed it back to the caller (view, test, etc).

I do not want these attribute modifications getting back into the database. However, I do want to include the id attribute in the cloned version, as it makes dealing with the route-helpers much easier. Thus, I plan on calling ActiveRecord::Base.clone(), manually setting the ID of the cloned instance, and then making the appropriate attribute changes to the new instance. This has me worried though; one save() on the modified instance and my original data will get clobbered.

So, I'm looking to lock down the new instance so that it won't hurt anything else. I'm already planning on calling freeze() (on the understanding that this prevents further modification to the object, though the documentation isn't terribly clear). However, I don't see any obvious way to prevent a save().

What would be the best approach to achieving this?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby