开发者

RoR ActiveRecord save method

开发者 https://www.devze.com 2023-03-21 05:10 出处:网络
Im new to Rails, im trying to execute the save method within an ActionController\'s Create method multiple times to insert multiple values

Im new to Rails, im trying to execute the save method within an ActionController's Create method multiple times to insert multiple values

def create

 开发者_开发百科 @pin = Pin.new(params[:pin])
  i = 1

  while i < 10
    if @pin.save
    end
  end

  redirect_to @pin

end

This works but only inserts one record there's no Contraints that enforces uniqueness of Records in my Database. Please how do i correct this?


One AR objects maps to one row. You need to create new object for each row you want added.

Something like that:

10.times do
  pin = Pin.new(params[:pin])
  pin.save
end

or

10.times do 
  Pin.create(params[:pin]
end

create method creates an AR object and saves it in the database. However, you cannot redirect to 10 objects.


you should extend your resource with create_multiple method and send params as array, see the details here

0

精彩评论

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