简体   繁体   English

如何使用哈希从数据库中的另一个记录创建的哈希在数据库中创建记录

[英]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.

相关问题 在Ruby on Rails 5中创建不带表单的数据库记录 - Create a record to the database without a form in Ruby on Rails 5 在rails中创建另一条记录时,使用外键创建关联记录 - Create associated record with foreign keys when another record is created in rails 使用 ruby​​ on rails 在单击按钮时从数据库中删除记录 - Delete record from database on clicking button using ruby on rails Ruby on Rails:如何 1)从 URL 中读取 JSON,然后 2)获取所需数据,并 3)添加创建新记录 - Ruby on Rails: How to 1) READ JSON from URL, then 2) GET the desired data, and 3) ADD create new record to the database Ruby on Rails:如何插入具有从数据库中获取值的记录 - Ruby on Rails : How to insert record with fetching value from database Ruby on Rails:从链接创建记录 - Ruby on Rails: Create record from link Android:如何在Android编程中编辑数据库中的特定记录(使用Ruby on Rails) - Android: How to edit specific record from database in Android Programming (Using Ruby on rails) 如何将数组中的记录与Ruby或Rails中的另一条记录进行比较? - how to compare a record inside an array with another record in Ruby or Rails? 创建记录后,Rails如何自动创建关联记录? - Rails, after a record is created, how to automatically create an associated record? Ruby on Rails 从 hash 递归地实例化活动记录模型 - Ruby on Rails instantiating active record models recursively from a hash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM