简体   繁体   中英

How to create a record in database using a hash which is created from another record in ruby on rails

I am having two rails systems, on one system I created a record using active record. Now I am converting that object to a Hash using attributes method like this user_hash = @user.attributes and sending it to other rails system.
Now I want to create user record on this system with this hash. The problem I am facing is that "id" field in database, how to override that field.
is there any way to solve this?

you can use clone also

# rails < 3.1
new_record = old_record.clone

#rails >= 3.1
new_record = old_record.dup

Returns a clone of the record that hasn't been assigned an id yet and is treated as a new record.

user_hash = @user.attributes
user_hash = ["id", "created_at", "updated_at"].each{ |k| user_hash.delete(k) }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM