Skip to content

Instantly share code, notes, and snippets.

@jacobo
Created February 3, 2014 19:57
Show Gist options
  • Save jacobo/8791215 to your computer and use it in GitHub Desktop.
Save jacobo/8791215 to your computer and use it in GitHub Desktop.
def load_from_cache
unless File.exists?(cache_path)
return false
end
toload = YAML.load_file(cache_path)
created = {}
models.each do |model|
toload[model.name].each do |id, attributes|
atts = {}
attributes.each do |k,v|
atts[k] = v
if k.to_s.match(/_id$/) && v
model.reflect_on_all_associations(:belongs_to).each do |assoc|
if assoc.foreign_key == k
search_in = created[assoc.class_name]
if found = (search_in && search_in[v.to_s])
atts[k] = found.id
else
binding.pry
end
end
end
end
end
created[model.name] ||= {}
created[model.name][id.to_s] = model.create!(atts)
end
end
true
end
def write_to_cache
File.open(cache_path, "w+") do |fp|
to_write = {}
models.each do |model|
model.all.each do |object|
to_write[model.name] ||= {}
to_write[model.name][object.id] = object.attributes
end
end
fp.write(to_write.to_yaml)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment