[英]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. 我有两个Rails系统,在一个系统上,我使用活动记录创建了一条记录。 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. 现在,我使用诸如user_hash = @user.attributes
类的属性方法将该对象转换为Hash并将其发送到其他Rails系统。
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. 我面临的问题是数据库中的“ id”字段,如何覆盖该字段。
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. 返回尚未分配id
且被视为新记录的记录的克隆。
user_hash = @user.attributes
user_hash = ["id", "created_at", "updated_at"].each{ |k| user_hash.delete(k) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.