How to Load Some Activerecord Models from a Yaml File and Save Them to the Db

How can I load some ActiveRecord models from a YAML file and save them to the DB?

I tried your scenario and I did not have any issues. I did the following changes to your YAML file creation logic:

yml_file = Rails.root.join('lib', 'tasks', 'questions', 'questions.yml')
File.open(yml_file, 'w') do |file|
questions = Question.order(:order_position).to_a
YAML::dump(questions, file)
end

I was able to retrieve the questions list as follows:

yml_file = Rails.root.join('lib', 'tasks', 'questions', 'questions.yml')
question_attributes_list = YAML.load_file(yml_file).map(&:attributes)
questions = Question.create(question_attributes_list)

Load an ActiveRecord models from a YAML file

With the YAML structure you added to your question the below should work, assuming file.yml is the name of you YAML file

require 'yaml' #I think rails does this already for you so it might not be necessary

def load_data_from_yaml
YAML.load_file("file.yml").each { |building| self.class.create!(building) }
end

if the size of your YAML file is huge and you want some speed you could have a look at: https://github.com/bjhaid/active_record_bulk_insert

and you method would look like this:

def load_data_from_yaml
self.class.bulk_insert(YAML.load_file("file.yml"))
end

Load seed data from YML to db with one-to-many relation in Rails

As discussed in chat, you'll need to use something like:

config[:projects].each do |project|
todos = project[:todos]
project.delete(:todos)
new_project = Project.create(project)
todos.each do |todo|
new_project.todos.create(todo)
end
end

That assumes your YML is formatted correctly.

How to turn Yaml file entires into models?

Write:

require 'yaml'

data = YAML.load_file 'filename.yml'

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

How to load the data from a .yml file to database?

Try this after loading the date from the yml file to records:

class Question < ActiveRecord::Base
# Question model just to import the yml file
end
records.each { |record| Question.create(record) }

You can simply create a model just for importing. You don't need to create the app/models/question.rb. Just write the code above in the script responsible for the importing.

UPDATE:

You can use the following function:

def create_class(class_name, superclass, &block)
klass = Class.new superclass, &block
Object.const_set class_name, klass
end

source

File.open("#{RAILS_ROOT}/db/fixtures/#{table_name}.yml", 'r') do |file|
YAML::load(file).each do |record|
model_name = table_name.singularize.camelize
create_class(model_name, ActiveRecod::Base) do
set_table_name table_name.to_sym
end
Kernel.const_get(model_name).create(record)
end
end

To use the connection directly you can use the following:

ActiveRecord::Base.connection.execute("YOUR SQL CODE")

How do I correctly format YAML to upload objects into my DB using frozen_record gem in Rails

There is proper format of the YAML file:

- id: 1
question_text: Why does this company need to buy your products or services?
answer_type: Text Field
next_question_id_yes: 2
next_question_id_no: ~
- id: 2
question_text: When do they need to have completed the project?
answer_type: Datetime
next_question_id_yes: 3
next_question_id_no: ~
- id: 3
question_text: What happens if they miss this deadline?
answer_type: Text Field
next_question_id_yes: 4
next_question_id_no: ~

How to seed join table data from yaml file through seed.rb in Rails

HABTM join tables do not usually have a model associated with them as they are simply join tables and nothing else.

That means these relationships would be established from one side or the other. Given my assumption that a Saft is more likely to have multiple Keywords I would go about this as follows.

I have no idea what a Saft is but if you know their ids I would recommend the following:

  1: 
- keyword1
- keyword2
- keyword3
2:
- keyword2
- keyword4

Then your seed file as:

  safts_keywords_data = YAML.load(Rails.root.join('db/seeds/safts_keywords_list.yml'))
safts_keywords_data.each do |saft_id,keywords|
saft = Saft.find(saft_id)
saft.keywords << keywords.map do |key|
Keyword.find_or_create_by(word: key)
end
end

If there is a clearer way to locate a unique Saft I would recommend that just from a readability standpoint but this should give you a clearer picture.

What is the standard way to dump db to yml fixtures in rails?

Heroku uses the YamlDB Gem

http://www.github.com/ludicast/yaml_db/tree/master



Related Topics



Leave a reply



Submit