... possibly a bit late for this, as Kevins answer is pretty much
identical anyway :), but I found this damned handy - but at the time
couldn't really find anything on this list to help before..
# get a record
@item = Item.first
# override original values where required
@duplicate = @item.attributes.merge({
:id => nil,
:type => 'duplicate',
:created_at => DateTime.now
})
......
You could also use the amazingly handy dm-is-self_referential [http://
github.com/snusnu/dm-is-self_referential] to say that the duplicate
you're creating is a 'child' of the original, if you so wished.. I
found this handy when specifying digital art prints that had both an
obverse and reverse pictures printed, e.g. The reverse is a child of
the obverse view :-)
Hope that helps as well..
On 14 July, 07:59, wea_gruena <[email protected]> wrote:
> Hi Kevin,
> thanks for your good idea.
>
> I've tried this:
>
> irb(main):001:0> require './db/config'
> => true
> irb(main):002:0> p1 = Profile.new
> => #<Profile @name=nil @descr=nil @pmodule_name=nil @standard=nil
> @active=nil @delete=nil @created_at=nil>
> irb(main):003:0> p1.name = 'TEST'
> => "TEST"
> irb(main):005:0> p1.pmodule_name = 'DESIGN'
> => "DESIGN"
> irb(main):006:0> p1.save
> => true
> irb(main):007:0> p2 = Profile.new
> => #<Profile @name=nil @descr=nil @pmodule_name=nil @standard=nil
> @active=nil @delete=nil @created_at=nil>
> irb(main):010:0> p2.attributes = p1.attributes
> =>
> {:descr=>nil, :pmodule_name=>"DESIGN", :active=>false,
> :created_at=>#<DateTime:
> 21214585031031383/8640000000,1/12,2299161>, :delete=>false, :name=>
> "TEST", :standard=>false}
> irb(main):011:0> p2.name = 'TEST_X'
> => "TEST_X"
> irb(main):012:0> p2.save
> => true
> irb(main):013:0>
>
> And it looks good.
> It's important to set the name (the key) AFTER "p2.attributes =
> p1.attributes", then everything it's O.K.
>
> Thanks a lot again,
> Andreas
>
> On 14 Jul., 00:25, Kevin <[email protected]> wrote:
>
> > Maybe you could get it to work with
>
> > p2.attributes = p1.attributes
> > p2.id = nil # so that it gets the auto-increment value from the
> > database/etc.
> > p2.save
>
> > Or use one of the strange hash.merge functions to combine your changes
> > to the p1.attributes.
>
> > Good luck!
> > -Kevin
>
> > On Jul 13, 6:20 am, wea_gruena <[email protected]> wrote:
>
> > > Hi all,
> > > the newbie is back ;-(
>
> > > Is it possible to create a copy of a record like this:
>
> > > [code]
> > > p1 = Profile.first(:name => 'ABC')
> > > p2 = p1.duplicate(:name => 'DEF')
> > > p2.save
> > > [/code]
>
> > > What's the best way to solve this problem ?
> > > Is it possible to copy / duplicate also associations etc. ?
>
> > > Thanks,
> > > Andreas
--
You received this message because you are subscribed to the Google Groups
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/datamapper?hl=en.