Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何复制或克隆模型?_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails 如何复制或克隆模型?

Ruby on rails 如何复制或克隆模型?,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我有一本带有属性id、名称和价格的模型书。我有一本书的实例: b1 = Book.new b1.name = "Blah" b1.price = 12.5 b1.save 我想复制b1,创建产品模型的另一个实例。我是tryid p1=b1.clone然后是p1.save,但它不起作用。有什么想法吗 我的环境是: Netbeans 6.9 RC2 JRuby 1.5.0 编辑: 我的临时产品型号: class Admin::TemporaryProduct < ActiveRecord

我有一本带有属性id、名称和价格的模型书。我有一本书的实例:

b1 = Book.new
b1.name = "Blah"
b1.price = 12.5
b1.save
我想复制b1,创建产品模型的另一个实例。我是tryid p1=b1.clone然后是p1.save,但它不起作用。有什么想法吗

我的环境是:

  • Netbeans 6.9 RC2
  • JRuby 1.5.0
编辑: 我的临时产品型号:

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
我想将b1的所有属性克隆到p1模型。

我想您需要:

b2 = Book.create(b1.attributes)
编辑: 考虑到上面的
创建
操作,我认为您要做的是将启动
@product
的行更改为

@temporary_products.each {|tp| Admin::Product.create(tp.attributes)}
这将为每个
TemporaryProduct
对象创建一个新的
Product
对象,使用与
TemporaryProduct
相同的属性。如果这不是你想要的,请告诉我。

我想你想要:

b2 = Book.create(b1.attributes)
编辑: 考虑到上面的
创建
操作,我认为您要做的是将启动
@product
的行更改为

@temporary_products.each {|tp| Admin::Product.create(tp.attributes)}

这将为每个
TemporaryProduct
对象创建一个新的
Product
对象,使用与
TemporaryProduct
相同的属性。如果这不是您想要的,请告诉我。

如果by不起作用,您的意思是数据库中没有新记录,那么您可能希望在保存之前将p1的id设置为null。如果克隆的id与原始的相同,则它似乎表示相同的对象。

如果by不起作用,则表示数据库中没有新记录,则可能需要在保存之前将p1的id设置为null。如果克隆与原始具有相同的id,则它似乎表示相同的对象。

例如,您可以使用rails中的
dup
创建重复记录

b1 = Book.create(name: "example", price: 120)
b1.save
duplicate_record = b1.dup
duplicate_record.save!
或者,您可以创建第一条新记录,然后进行复制


希望这对您有用。

您可以使用rails中的
dup
来制作重复记录

b1 = Book.create(name: "example", price: 120)
b1.save
duplicate_record = b1.dup
duplicate_record.save!
或者,您可以创建第一条新记录,然后进行复制



希望这对您有用。

b1.attributes函数未找到!b1=Admin::TemporaryProduct,p1=Admin::Productb1=Admin::TemporaryProduct.new,p1=Admin::Product.newb1.attributes未找到函数!b1=Admin::TemporaryProduct,p1=Admin::Productb1=Admin::TemporaryProduct.new,p1=Admin::Product.new在您发布的
创建
操作中,
@temporary\u products
将是
Admin::TemporaryProduct
类型的对象数组<代码>属性是单个实例的方法。我不太清楚您是想克隆所有结果,还是只克隆一个,或者您是否希望
find\u all\u by\u original\u product\u id
只返回一个结果。我不这么认为。它返回一个数组。是的,我知道它返回一个数组。因此,
@temporary\u products.attributes
将不起作用,因为
.attributes
是单个实例的方法。你是想克隆所有返回的临时产品,还是只克隆一个?好的,我明白了。我只是想克隆所有的临时产品。但这是工作。我得到一个错误。我将在下面编辑我的答案。在你发布的
create
操作中,
@temporary\u products
将是一个类型为
Admin::temporary product
的对象数组<代码>属性是单个实例的方法。我不太清楚您是想克隆所有结果,还是只克隆一个,或者您是否希望
find\u all\u by\u original\u product\u id
只返回一个结果。我不这么认为。它返回一个数组。是的,我知道它返回一个数组。因此,
@temporary\u products.attributes
将不起作用,因为
.attributes
是单个实例的方法。你是想克隆所有返回的临时产品,还是只克隆一个?好的,我明白了。我只是想克隆所有的临时产品。但这是工作。我得到一个错误。我将在下面编辑我的答案。我认为这是最惯用的答案。我认为这是最惯用的答案。