急!Ruby On Rails 同时插入多条数据的问题
背景:
我在给一张表(Script)里追加一条数据的时候,要给附属表(Progress)要同时追加插入n条数据。
model:
class Script < ActiveRecord::Base
has_many :progresses,:dependent => :destroy
class Progress < ActiveRecord::Base
belongs_to :script
在Script的Controller里的create方法里:
@script = Script.new(params[:script])
@script.save!
@script.progresses.create([{ :percent => 0,:object_id => "1" :percent => 1, :object_id => "2"}])
可是执行之后发现附属表(Progress)里只插入了一条数据。请问是那里的问题啊?
api里的例子是:
------
# Create an Array of new objects
User.create([{ :first_name => 'Jamie' }, { :first_name => 'Jeremy' }])
------
请教大家了!
[解决办法]
明显API写的是两个数组参数,
User.create([{ :first_name => 'Jamie' }, { :first_name => 'Jeremy' }])
你却是一个
改一下:
@script.progresses.create([{ :percent => 0,:object_id => "1"} , {:percent => 1, :object_id => "2"}])