开发者

Save an array of models in rails

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜