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-circleimageimagesinstagramFill 1linklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

Re: [ruby-81] copying and comparing model objects

From: Loqi
Sent on: Tuesday, March 4, 2008 1:15 PM
>They're only editing @model.attributes, really, so just .dup the
>hashes and use Hash#diff recursively. No?

Thanks, Ian. I somehow missed that stuff in my studies.

Does this code look right to you? It's adapted from the Depot example 
of the Agile Web Development book.

>  def edit
>    @product = Product.find(params[­:id])
>    @product_attributes_­before = @product.attributes # <<<<<­<<<<<­<< right?
>  end
>
>  def update
>    if @product.attributes.­diff(@product_attrib­utes_before) == {} # 
><<<<­<<<<<­<< right?
>      flash[:notice] = 'You haven't changed anything.'
>      redirect_to :action => 'show', :id => @product
>    else
>      @product = Product.find(params[­:id])
>      if @product.update_attr­ibutes(params[:produ­ct])
>        flash[:notice] = 'Product was successfully updated.'
>        redirect_to :action => 'show', :id => @product
>      else
>        render :action => 'edit'
>      end
>    end
>  end

Our Sponsors

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