开发者

Rails inserting multiple records for single model

开发者 https://www.devze.com 2023-01-31 08:03 出处:网络
How shall I set the form fields to be able to insert multiple rows in the database for a single model.

How shall I set the form fields to be able to insert multiple rows in the database for a single model.

I am updating a div with another link and cannot use the form helper. So I need to set the field names manually.

I have a post model and it has a title field. I want to insert i posts to db like post[0][title] But when I name the form field like this It gets 0 as string and does not record.

Also I tried to set the Array my self from Rails Console like

post = Arra开发者_开发技巧y.new
post << [:title => "title 1"]
post << [:title => "title 2"]
sav = Post.new(post)
sav.save 

And still nothing is saved.


posts = Array.new
posts << {:title => "title 1"}
posts << {:title => "title 2"}
Post.create(posts)


is this what you're trying to do?

posts = []
posts << Post.new(:title => "title 1")
posts << Post.new(:title => "title 2")

posts.each do |post|
  post.save
end
0

精彩评论

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

关注公众号