I am working on a Ruby script that will dump a key, value pair into a yaml file. But for some reason my loop is only grabbing the last instance in the loop. There should be multiple key value pairs.
Code:
# Model for languages Table
class Language < ActiveRecord::Base
end
# Model for elements Table
class Element < ActiveRecord::Base
has_many :element_translations
end
# Model for element_translations Table
class ElementTranslation < ActiveRecord::Base
belongs_to :element
end
# Find ALL languages
lang = Language.all
# Get all elements
elements = Element.where("human_readable IS NOT NULL")
info = ''
elements.each do |el|
lang.each do |l|
et = el.element_translations.where("language_id = ?", l.id)
et.each do |tran|
info = {
el.human_readable.to_s => tran.content.to_s
}
end
File.open(l.code.to_s + ".yml", "w", :encoding => "UTF-8") do |f|
YAML.dump(info, f)
end
end
end
Any ideas?
When you do:
did you mean:
Otherwise you’re just re-assigning
infoeach time.If you’re going to do this, make info an array:
info = []as opposed toinfo = ''.