gpt4 book ai didi

ruby-on-rails - 在 rails 中将一个表记录复制到另一个表记录

转载 作者:数据小太阳 更新时间:2023-10-29 07:23:05 25 4
gpt4 key购买 nike

我有一个User 模型和users 表。一个用户可以有很多电话号码,所以我有一个名为 Phone 的单独模型。我为此使用了这个协会:

模型

User
attr_accessible :id, :name, :screenname,:fullname,:phones_attributes
has_many :phones,:dependent => :destroy

Phone
attr_accessible :phone
belongs to :users

以上代码运行良好。 管理员也想将任何用户的记录复制到 user_tempphone_temp 表中(我有单独的模型名为 UserTempPhoneTemp).

我该怎么做?

最佳答案

最简单的方法是:

phone_item = Phone.find(x)   # Get the phone item you want to copy
# you may have obtained this some other way

PhoneTemp.create(phone_item.attributes) if phone_item

对于用户也是如此。

关于ruby-on-rails - 在 rails 中将一个表记录复制到另一个表记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18786685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com