开发者

Save an array of models in rails

开发者 https://www.devze.com 2023-03-22 07:23 出处:网络
I have an array that keeps some instances of a model (SupplierPhone) like this: supplier_phone = SupplierPhone.where(:supplier_id => 1)

I have an array that keeps some instances of a model (SupplierPhone) like this:

supplier_phone = SupplierPhone.where(:supplier_id => 1)
supplier_phone =  [#<SupplierPhone id: 1, supplier_id: 1, phone: "22423434234",
created_at: "2011-07-21 00:56:26", updated_at: "2011-07-21 00:56:26">, 
#<SupplierPhone id: 2, supplier_id: 1, phone: "98开发者_开发问答776234423", 
 created_at: "2011-07-21 00:56:27", updated_at: "2011-07-21 00:56:27">] 

I want to save each element of the array supplier_phone in a new instance of SupplierPhone, can somebody tell me how can I do this?

Thanks in advance!


supplier_phone.each{ |sp| SuplierPhone.create(sp.attributes) }


you can use associations. supplier has_many :phones", then get the phones through a chain "supplier.phones"

0

精彩评论

暂无评论...
验证码 换一张
取 消