Gag*_*gan 4 ruby ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2
我有一个用户模型和 用户表.用户可以拥有多个电话号码,因此我有一个名为的单独模型Phone.我正在使用这个关联:
模型
User
attr_accessible :id, :name, :screenname,:fullname,:phones_attributes
has_many :phones,:dependent => :destroy
Phone
attr_accessible :phone
belongs to :users
Run Code Online (Sandbox Code Playgroud)
以上代码工作正常.管理员想要将任何用户的记录复制到user_temp和phone_temp表中(我有单独的模型名为UserTemp和PhoneTemp).
我怎样才能做到这一点?
lur*_*ker 10
最简单的方法是:
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
Run Code Online (Sandbox Code Playgroud)
同样对于用户.