ruby - How to duplicate object "by value" in Rails? -


i need duplicate product in spree application. so

def my_duplicate_product(product)   product.dup.tap |new_product|     new_product.slug = "#{product.slug}-#{rand(1000)}"     ... 

this code causes original product#slug changed.

what supposed copy of particular product , leave original unchanged?

rails version: 4.0.3

update:

the problem not in ruby, nor rails — it's globalize gem (v. 4.0.0).

this error fixed in 4.0.3.

it realy broke #dup values "shared" between original model , duplicated one.

see github issue tracker more information: https://github.com/globalize/globalize/pull/352

maybe clone works:

def my_duplicate_product(product)   product.clone.tap |new_product|     new_product.slug = "#{product.slug}-#{rand(1000)}" 

clone method on ruby doc


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -