【发布时间】:2020-07-13 21:22:47
【问题描述】:
我有一个对象 Persons,它是一个 ActiveRecord 模型,其中包含一些字段,例如 :name、:age 等。
Person 与称为 Account 的东西之间存在一对一的关系,其中每个人都有一个帐户。
我有一些代码:
Account.create!(person: current_person)
其中 current_person 是指定的现有 Person 活动记录对象。
注意:表 Account 有一个字段为 person_id 并且他们两个在模型中都有 has_one。
现在我相信我们可以像下面这样进行批量创建:
Account.create!([{person: person3},{person:: person2} ....])
我有一个 persons 数组,但不确定转换为所有具有相同键的哈希数组的最佳方法。
基本上Convert array of hashes to array的反面是我想做的。
【问题讨论】:
标签: arrays ruby-on-rails ruby activerecord hash