Help with inserting multiple records via console

16 views
Skip to first unread message

captain awesome

unread,
May 23, 2012, 8:54:31 PM5/23/12
to rubyonra...@googlegroups.com
I'm fairly new to Rails and was wondering would it be possible to insert multiple records via the console?

I tried variations along the lines of this but all failed.
Article.create {:title => "Test title 1", :body => "Test body text 1, ..", :published_at => Date.today} {:title => "Test title 2", :body => "Test body text 2..", :published_at => Date.today}

If this is possible, please help.

Thank you

Phil Crissman

unread,
May 24, 2012, 6:40:36 PM5/24/12
to rubyonra...@googlegroups.com
Not exactly, you could create an array of params hashes and iterate over it. Using your example, it would be something like: 

[{:title => "Test title 1", :body => "Test body text 1, ..", :published_at => Date.today}, {:title => "Test title 2", :body => "Test body text 2..", :published_at => Date.today}].each{ |params| Article.create(params) }

At some point you might want to look into gems like factory_girl; usually used for testing, but could also be used to seed a development environment with a little more convenience.

Phil



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/R1JqmUkh-J8J.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply all
Reply to author
Forward
0 new messages