addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupshelp-with-circleimageimagesinstagramlinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

[ruby-81] copying and comparing model objects

From: Loqi
Sent on: Tuesday, March 4, 2008 11:05 AM
This seems like something that should be trivial, but I'm not 
satisfied with any of the solutions I've tried.

I want to read an Active Record model into two identical memory 
objects, allowing the user to edit one of them. Then when it comes 
time to write, I'll compare the two objects' data, only updating the 
DB if there's been a change.

The easiest way to deep-copy DB models is to simply find() them 
twice, but that's just wrong. Serializing the object with Marshal 
would work for copying, but it's unsatisfying, and comparing two 
serializations seems as though it would cause false negatives. I 
could roll my own recursive copy and compare methods, and attaching 
them to ActiveRecord::Base or to Object, but this is probably 
something that's already been done better by someone else.

Any ideas on a hassle-free way to do a deep copy and deep compare on 
two model objects?

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy