ruby on rails - How to copy or clone model? -


I have a model book with the attributes ID, name, value. I have an example of a book:

B1 = Book.new b1.name = "blah" b1.price = 12.5 b1.save

I would like to copy B1, the product model's Make another example. I'm trying p1 = b1.clone then p1.save but it did not work.

  • Any ideas?

    And my environment is:

    • Netbeans 6.9 RC2
    • JRuby 1.5.0

    Edited: My Temporary Product Model:

      Category Admin: Temporary Products & lt; ActiveRecord :: Base Df self.update_from_web_service (web_service_url) Feedback = HTTParty.get (web_service_url) Feedback ["webServiceResult"] ["Product"]. Every do Element | Until it does not exist? : Orignal_product_id = & gt; Make the element ['id']! Element ['name'],: value => element ['value'] ,: zodiac => element ['sum'],: description => element ['description Element ['id'],: image => element ['image'],: shop_account_number = & gt; element ['shopAccountNumber'], unit = & gt; element [' Unit ']) End And End End  

    Creating product action:

      def Make @ememporary_products = Admin :: TemporaryProduct.find_all_by_orignal_product_id (params [ : Products]) @product = admin :: product.new (@ temp_products.features) # @product = @ temporary_products.clone Answer_to dot Format | Format.html {redirect_to (admin_products_url ,: notice = & gt; Administrator :: product was successfully created.}} End End  

    I clone the properties of all B1 models of P1 models I want to do that.

  • I think you want:

      B2 = Book.create (b1 .attributes)  

    Edit: Looking at your build action above, I think What you want to do is change the row that starts @product

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

    This is each temporary product product object for code> object temporary product if the same features are not what you want, then let me know.


    Comments

    Popular posts from this blog

    windows - Heroku throws SQLITE3 Read only exception -

    lex - Building a lexical Analyzer in Java -

    python - rename keys in a dictionary -