2

I have several deeply nested models and I would like to seed my database. The models are as following: Each restaurant has many menus. Each menu has many categories. Each category has many meals. Currently, my seed looks like this:

restaurant_seed = [
  {
    name: "KFC",
    address: "Sofia",
    description: "Fast food.",
    phone_number: "88888888"
  }
]

menu_seed = [
  {
    name: 'Spring menu.',
    active: true
  },
  }
    name: 'Winter menu.',
    active: false
  }
]

category_seed = [
  {
    name: "Dessert",
    available_all_day: false,
    age_restriction: false,
    category_avatar: File.open(File.join(Rails.root, "app/assets/images/desserts.jpg"))
  },
  {
    name: "Salad",
    available_all_day: true,
    age_restriction: false,
    category_avatar: File.open(File.join(Rails.root, "app/assets/images/salads.jpeg"))
  }
]

meal_seed = [
  {
    name: "Shopska salata",
    meal_avatar: File.open(File.join(Rails.root, "app/assets/images/shopska_salad.jpg"))
  },
  {
    name: "Shisha",
    meal_avatar: File.open(File.join(Rails.root, "app/assets/images/shisha.jpg"))
  }
]

However, I do not know how to actually seed the database with that info. The idea is that each restaurant will have all of the menu seeds, each of the menus in each restaurant will have each category from the category seed and so on. Thank you for any suggestions!

Vucko
  • 20,555
  • 10
  • 56
  • 107
  • 2
    http://stackoverflow.com/questions/4419672/how-to-dbseed-a-model-and-all-its-nested-models – Aleksei Matiushkin Jun 14 '16 at 07:34
  • 1
    Just create the objects using Active Record. eg `Restaurant.create(restaurant_attrs_here)` – Taryn East Jun 14 '16 at 07:38
  • That would work in case there is only one restaurant. However, I will add more and if I follow mudasobwa's answer, if I have 5 restaurants, 6 menus, 7 categories, 8 meals, I would have to write about 5*6*7*8 lines of code. – Kristiyan Tsvetanov Jun 14 '16 at 07:50
  • 4
    `I would have to write about 5*6*7*8 lines of code` -- This is software... You can write CODE! Use methods, classes, loops, whatever you need... Don't just copy+paste something 1680 times!! – Tom Lord Jun 14 '16 at 08:11
  • Tom Lord, that's what I asked for. How to write these lines of code the smart way? – Kristiyan Tsvetanov Jun 14 '16 at 11:20
  • I found [mechanize](https://github.com/sparklemotion/mechanize) to be great as you can seed through lists into your set up there is a tutorial available [here](https://www.youtube.com/watch?v=i1bAZEvoYCg) – Artem Ankudovich Jun 14 '16 at 14:43
  • Also please consider using a CDN for images Cloudinary provides free accounts – Artem Ankudovich Jun 14 '16 at 14:44

1 Answers1

1

Write a method to iterate all seeds and create corresponding records.

def setup_restaurants(restaurant_seed, menu_seed, category_seed, meal_seed)
  restaurant_seed.each do |r_seed|
    restaurant = Restaurant.create(r_seed)
    menu_seed.each do |m_seed|
      menu = restaurant.menus.create(m_seed)
      category_seed.each do |c_seed|
        category = menu.categories.create(c_seed)
        meal_seed.each do |mm_seed|
          category.meals.create(mm_seed)
        end
      end
    end
  end
end
Larry Lv
  • 759
  • 4
  • 10