Skip to content

Instantly share code, notes, and snippets.

@potatosalad
Forked from Chris-Galten/gist:1528941
Created December 28, 2011 18:16
Show Gist options
  • Save potatosalad/1528977 to your computer and use it in GitHub Desktop.
Save potatosalad/1528977 to your computer and use it in GitHub Desktop.
Making categories
step 'the following category records' do |table|
table.hashes.each do |hash|
a = AttributeSet.find_or_create_by(slug: 'myattritubetest', label: 'myattributetest', description: 'myattributeset')
if hash['parents'].present?
path = hash['parents'].split(', ')
ancestor = Category.find_or_create_by(title: path.shift, attribute_set_id: a.id)
path.each_with_index do |ancestor_title, index|
category = Category.find_or_create_by(title: ancestor_title, parent_id: ancestor.id, attribute_set_id: a.id)
ancestor = category
end
else
Category.find_or_create_by(title: hash['category'], attribute_set_id: a.id)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment