Search code examples
ruby-on-railsrubyclonesequel

Clone Sequel Model


I have a Model representing a Pricing Table with lots of entries and i want to offer the possibility to create a new Pricing with values from an existing entry.

Does anyone know how to do this, then Sequel is in use?

I tried dup and clone but in both cases the id is still there from the existing Model and thus will update the existing entry.

If i try to set the id by hand i get following error:

Sequel::InvalidValue: nil/NULL is not allowed for the id column

So i need a to find a Way to create a Model which is new but has prefilled values without having them to set in the code by hand.

Any ideas?


Solution

  • found it:

    new_pricing = Pricing.new(oldprice.attributes.tap{|attr| attr.delete("id")})
    

    i get the attributes from the old model as hash, then remove the id and create a new Model by passing the attributes except the id.