[英]How to create a record in database using a hash which is created from another record in ruby on rails
我有兩個Rails系統,在一個系統上,我使用活動記錄創建了一條記錄。 現在,我使用諸如user_hash = @user.attributes
類的屬性方法將該對象轉換為Hash並將其發送到其他Rails系統。
現在,我想使用此哈希在此系統上創建用戶記錄。 我面臨的問題是數據庫中的“ id”字段,如何覆蓋該字段。
有什么辦法解決這個問題?
你也可以使用克隆
# rails < 3.1
new_record = old_record.clone
#rails >= 3.1
new_record = old_record.dup
返回尚未分配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.