Rails: Copying Attributes from an Object to Another Using the "Attributes" Method

Rails: Copying attributes from an object to another using the attributes method

You can select only the attributes that Quote has:

Quote.new(invoice.attributes.select{ |key, _| Quote.attribute_names.include? key })

As noted by @aceofspades (but not with a dynamic solution), you can use ActiveSupport's slice as well:

Quote.new(invoice.attributes.slice(*Quote.attribute_names))

Rails Copying attributes from User object to new object

You were on the right track with the first implementation. You just have to use self instead of user.

ArchivedUser.create(self.attributes.slice(ArchivedUser.attribute_names))

How to copy a attribute of another model in rails?

There are two things missing in your code:

  1. self is needed to set the value for email.
  2. The object needs to be saved after setting email.

So, after adding self and save, your get_email method should look like this:

def get_email
self.email = user.email
save
end

Notice that also dup has been removed, since it is not needed to copy the value (more information about dup here).


That said, i recommend using before_create action instead of after_create:

class Car < ActiveRecord::Base
belongs_to :user
attr_accessible :email, :engine

before_create :get_email

def get_email
self.email = user.email
end
end

With :before_crete you will copy the email from User and only will need to save your object once. If you do it with :after_create, first you will save the object, then look for the email in User and then will need to execute an additional update (i.e save again).

Duplicate an object while changing one of its attributes

If copying only attributes but associations is ok for you, then you are doing good. http://apidock.com/rails/ActiveRecord/Core/dup

But, I suggest you use assign_attributes, so you'll make only one query.

def add_items_to_cart(cart, cart_user)
cart.update_attributes(merchant_id: self.merchant_id)

self.cart_items.each do |ci|
new_cart_item = ci.dup
new_cart_item.assign_attributes(cart_id: cart.id, cart_user_id: cart_user.id)
new_cart_item.save
end
end

EDIT:

Make a method Cart#duplicate, which returns what you need

class CartItem      

...

# returns copy of an item
def duplicate
c = Cart.new cart_id: cart.id, cart_user_id: cart_user.id
# copy another attributes inside this method
c
end
end


# And use it
self.cart_items.each do |ci|
new_card_item = ci.duplicate
new_card_item.save
end

How do I clone a Rails model attribute?

I ended up making copies of each of the fields I wanted to keep track of:

@oldUsername = @user.username.clone

User.new looked promising, but it treated the copy as a new object, when it was an existing model, and output invalid forms to edit the model in the views:

> app.controller.view_context.form_for u2 do end   # This is from Rails console
=> "<form accept-charset=\"UTF-8\" action=\"/users\" class=\"new_user\" id=\"new_user_1\" method=\"post\">

So it would attempt to PATCH to /users (from the view), which is invalid, when it should PATCH to /users/1/.

It's unbelievable that Rails won't clone objects correctly. In Java, you could use u2.setProperty( u.getProperty().clone() ) and be sure to have a new object that won't interfere with the old one.

Copy model instances in Rails

This is what ActiveRecord::Base#clone method is for:

@bar = @foo.clone

@bar.save


Related Topics



Leave a reply



Submit