1

I wrote a small ruby script that generates an array like the one below:

{:title=>"Lorem ipsum",
 :category=>["Lorem ipsum"],
 :items=>
  ["Lorem ipsum",
   "Lorem ipsum",
   "Lorem ipsum"],
 :process=>
  ["Sed ut perspiciatis unde omnis iste natus error."]}

How can I run that script in order to save the output into my rails database?

Antonio Lopes
  • 512
  • 2
  • 8
  • 21
  • What Models do you have in your app? are these items stored under a different model? – Dogbert Jan 16 '11 at 18:48
  • @adam: I have 3 models. Model "A" => Title and Process Model "B" => Items Model "C" => Category – Antonio Lopes Jan 16 '11 at 18:51
  • 1
    I think you mean "hash", not array. you say model A holds title and process, but then you use an array for process. On the other hand, what's the nature of the problem, this seems pretty straighforward AR usage, did you peruse the Rails guides? – tokland Jan 16 '11 at 18:59

1 Answers1

1

With a bit of guessing... you can write this is a rake task:

WeDontKnowWhichModelHere.create!({
 :title => "Lorem ipsum",
 :categories => [Category.new(:name => "Lorem ipsum"), ...],
 :items => [Item.new(:name => "Lorem ipsum"), ...],
 :processes => [Process.new(:name => "Sed ut perspiciatis unde omnis iste natus error")],
})
tokland
  • 66,169
  • 13
  • 144
  • 170
  • Where in the project should I call my script.rb in order to gather the output? Thank you – Antonio Lopes Jan 16 '11 at 19:01
  • @antonio. If this is a code outside the request cycle, you can write a rake task: http://stackoverflow.com/questions/876396/do-rails-rake-tasks-provide-access-to-activerecord-models, http://railscasts.com/episodes/66-custom-rake-tasks – tokland Jan 16 '11 at 19:03