I have a model Book with attributes id, name, price. I have an instance of Book:
b1 = Book.new
b1.name = "Blah"
b1.price = 12.5
b1.save
I would like to copy b1, create another instance of the Product model. I'm tryid p1=b1.clone then p1.save but it didn't work. Any idea?
And my environment is:
- Netbeans 6.9 RC2
- JRuby 1.5.0
EDITED: My TemporaryProduct model:
class Admin::TemporaryProduct < ActiveRecord::Base
def self.update_from_web_service(web_service_url)
response = HTTParty.get(web_service_url)
response["webServiceResult"]["product"].each do|element|
unless exists? :orignal_product_id => element['id']
create!(
:name => element['name'],
:price => element['price'],
:amount => element['amount'],
:description => element['description'],
:orignal_product_id => element['id'],
:image => element['image'],
:shop_account_number => element['shopAccountNumber'],
:unit => element['unit']
)
end
end
end
end
Product is create action:
def create
@temporary_products = Admin::TemporaryProduct.find_all_by_orignal_product_id(params[:product])
@product = Admin::Product.new(@temporary_products.attributes)
# @product = @temporary_products.clone
respond_to do |format|
format.html { redirect_to(admin_products_url, :notice => 'Admin::Product was successfully created.') }
end
end
I want to clone all b1's attributes to p1 model.