Josh
Josh

Reputation: 5721

How to turn Yaml file entires into models?

I want to create a landing page for internal apps that updates itself so I have a gem in place that polls the file where apps are stored each night and then spits out a yaml file with the latest information.

Here is what the yaml file looks like:

Applications:
  App1:
    Name: name1
    Link: link1
  App2
    Name: name2
    Link: link2

I want to sort through the yaml file and create a model for each 'app' entry that I can manipulate but I have no idea how I could do that or where to place that logic. I think it may go in the seed.rb file but then would I have to run rake db:seed each time someone visited the page?

Upvotes: 0

Views: 2052

Answers (1)

Hauleth
Hauleth

Reputation: 23566

Write:

require 'yaml'

data = YAML.load_file 'filename.yml'

data['Applications'].each do |key, values|
  Application.create values
end

Upvotes: 2

Related Questions