ga-wdi-exercises / project2

[project]
1 stars 30 forks source link

Having problems with my many-to-many associations #878

Closed electricflannel closed 7 years ago

electricflannel commented 7 years ago

I am getting this error when I try to seed my data. I have reset database and everything migrates over just fine. I have verified the naming and it looks like everything is correct. Is there anything I am missing?

ActiveRecord::RecordNotSaved: You cannot call create unless the parent is saved

This is how I have my files configured. ./app/models/tag.rb

class Tag < ApplicationRecord
    has_many :recipe_tags
    has_many :recipes, through: :recipe_tags
end

./app/models/recipe.rb

class Recipe < ApplicationRecord
    default_scope { order('updated_at DESC') }
    validates :title, presence: true
    validates :img_url, presence: true
    validates :description, presence: true
    has_many :ingredients, dependent: :destroy
    has_many :directions, dependent: :destroy
    belongs_to :user
    has_many :recipe_tags
    has_many :tags, through: :recipe_tags
 end

./app/models/recipe_tag.rb

class RecipeTag < ApplicationRecord
    belongs_to :tag
    belongs_to :recipe
end

./db/migrate/20170501171404_create_tags.rb

class CreateTags < ActiveRecord::Migration[5.0]
  def change
    create_table :tags do |t|
        t.string :tag
      t.timestamps
    end
  end
end

./db/migrate/20170501171730_create_recipe_tag.rb

class CreateRecipeTag < ActiveRecord::Migration[5.0]
  def change
    create_table :recipe_tags do |t|
        t.references :tag, index: true, foreign_key: true, null: false
      t.references :recipe, index: true, foreign_key: true, null: false
    end
  end
end

./db/seeds

Recipe.destroy_all
Direction.destroy_all
Recipe.destroy_all
Tag.destroy_all
RecipeTag.destroy_all

spaghetti = Recipe.create(title: "Spaghetti", description: "Spaghetti with Meat Sauce is the perfect simple weeknight meal using only ONE pot! Everyone will rave about this easy dinner dish!", img_url: "http://www.lovethispic.com/uploaded_images/202640-Spaghetti-With-Mediterranean-Roasted-Tomato-Sauce.jpg")
buffalo_macaroni = Recipe.create(title: "Buffalo Macaroni", description: "This kid-friendly, healthy easy mac and cheese recipe is the only macaroni recipe you'll need, take it from a mac cheese connoisseur! Vegan, gluten-free.",  img_url: "https://s-media-cache-ak0.pinimg.com/736x/3e/89/bb/3e89bbae8a3ba68ffc1f33929edae5c9--baked-macaroni-cheese-mac-cheese.jpg")
mexican_tacos = Recipe.create(title: "Mexican Tacos", description: "Thin steak about 2 lbs. Authentic Mexican tortillas for tacos they are small. Cut your limes in half and then in 4's. Cut your steak in strips then small peaces like this.", img_url: "https://s-media-cache-ak0.pinimg.com/736x/f9/5a/e5/f95ae5dd56e2007ebab46310e808b996.jpg")
greased_cassarole = Recipe.create(title: "Greased Cassarole with Slices of Lemon Juice", description: "Thin steak about 2 lbs. Authentic Mexican tortillas for tacos they are small. Cut your limes in half and then in 4's. Cut your steak in strips then small peaces like this.", img_url: "http://heatherlikesfood.com/wp-content/uploads/2012/08/Tuna-Noodle-web.jpg")

spaghetti.directions.create(step_num: 1, step: "Heat a second large pot over medium-high heat. Add the beef, 1/2 teaspoon salt and a few grinds of pepper and cook, stirring to break the meat up, until browned, about 4 minutes. Add the onions, bell peppers, garlic and a generous pinch of salt and pepper and cook, stirring occasionally, until tender, about 10 minutes.")
spaghetti.directions.create(step_num: 2, step: "Meanwhile, cook the Dor 2 minutes less than the time indicated in the package spaghetti1 = direction. Strain well. Preheat the oven to 350 degrees F.")
spaghetti.directions.create(step_num: 3, step: "Cover the bottom of a 13-by-9-by-2-inch pan with a third of the sauce. Add half the pasta and a third of the Cheddar; repeat the layers and add the last of the sauce; reserve the last of the Cheddar.")
spaghetti.directions.create(step_num: 4, step: "Heat a second large pot over medium-high heat. Add the beef, 1/2 teaspoon salt and a few grinds of pepper and cook, stirring to break the meat up, until browned, about 4 minutes. Add the onions, bell peppers, garlic and a generous pinch of salt and pepper and cook, stirring occasionally, until tender, about 10 minutes.")
spaghetti.directions.create(step_num: 5, step: "Meanwhile, cook the Dor 2 minutes less than the time indicated in the package spaghetti1 = direction. Strain well. Preheat the oven to 350 degrees F.")
spaghetti.directions.create(step_num: 6, step: "Cover the bottom of a 13-by-9-by-2-inch pan with a third of the sauce. Add half the pasta and a third of the Cheddar; repeat the layers and add the last of the sauce; reserve the last of the Cheddar.")

buffalo_macaroni.directions.create(step_num: 1, step: "Heat oven to 425°F. Cook and drain macaroni as directed on package, using minimum cook time.")
buffalo_macaroni.directions.create(step_num: 2, step: "Meanwhile, in 3-quart saucepan, melt butter over low heat. Stir in flour and mustard with whisk; cook and stir until smooth. Gradually add milk. Heat to boiling, stirring constantly. Boil 1 minute until mixture is hot and bubbly. Add Cheddar cheese; stir until cheese is melted and sauce is smooth. Season with salt.")
buffalo_macaroni.directions.create(step_num: 3, step: "Gently stir cooked macaroni into cheese sauce. Stir in chicken. Spoon into ungreased 8-inch square (2-quart) glass baking dish. Sprinkle celery and blue cheese over macaroni mixture. Drizzle with Buffalo wing sauce.")
buffalo_macaroni.directions.create(step_num: 4, step: "Meanwhile, in 3-quart saucepan, melt butter over low heat. Stir in flour and mustard with whisk; cook and stir until smooth. Gradually add milk. Heat to boiling, stirring constantly. Boil 1 minute until mixture is hot and bubbly. Add Cheddar cheese; stir until cheese is melted and sauce is smooth. Season with salt.")
buffalo_macaroni.directions.create(step_num: 5, step: "Gently stir cooked macaroni into cheese sauce. Stir in chicken. Spoon into ungreased 8-inch square (2-quart) glass baking dish. Sprinkle celery and blue cheese over macaroni mixture. Drizzle with Buffalo wing sauce.")

mexican_tacos.directions.create(step_num: 1, step: "Place the ground beef in a skillet over medium heat, and sprinkle about 3/4 of the packet of taco seasoning over the meat. Cook and stir the ground beef, breaking it up as it cooks, until the beef is browned and crumbly, 10 to 15 minutes. Drain the excess grease.")
mexican_tacos.directions.create(step_num: 2, step: "Mash the refried beans with the water in a small saucepan, and sprinkle with the rest of the seasoning packet. Heat the refried beans over low heat until simmering.")
mexican_tacos.directions.create(step_num: 3, step: "Preheat oven to 300 degrees F (150 degrees C). Place the crisp taco shells on a baking sheet, and warm them in the preheated oven for 3 to 5 minutes.")
mexican_tacos.directions.create(step_num: 4, step: "Mash the refried beans with the water in a small saucepan, and sprinkle with the rest of the seasoning packet. Heat the refried beans over low heat until simmering.")
mexican_tacos.directions.create(step_num: 5, step: "Preheat oven to 300 degrees F (150 degrees C). Place the crisp taco shells on a baking sheet, and warm them in the preheated oven for 3 to 5 minutes.")

greased_cassarole.directions.create(step_num: 1, step: "Add the other ingredients except the electrician")
greased_cassarole.directions.create(step_num: 2, step: "Spread hot milk over the fire until it begins to spin and boil. Add a dash red water.")
greased_cassarole.directions.create(step_num: 3, step: "The water is often accompanied by some sort of uncolored lemon or orange or canned lobster or canned fish or canned milk or three eggs.")
greased_cassarole.directions.create(step_num: 4, step: "In deep enough water the eggs are usually called the crabs. Be sure to milk the eggs until they turn into fish mixture.")
greased_cassarole.directions.create(step_num: 5, step: "The mixture is quite soft but not so oily that it destroys most people unacquated with mayonaise.")

spaghetti.ingredients.create(ingredient_name: "Cheese", amount: "3 cups")
spaghetti.ingredients.create(ingredient_name: "Sauce", amount: "2 cups")
spaghetti.ingredients.create(ingredient_name: "Pasta", amount: "2 handfuls")
spaghetti.ingredients.create(ingredient_name: "Garlic cloves", amount: "3 cups")
spaghetti.ingredients.create(ingredient_name: "Onion", amount: "1")
spaghetti.ingredients.create(ingredient_name: "Oregano", amount: "1 Tsp")

buffalo_macaroni.ingredients.create(ingredient_name: "Macaroni", amount: "5 cups")
buffalo_macaroni.ingredients.create(ingredient_name: "Vanilla", amount: "2 tbsp")
buffalo_macaroni.ingredients.create(ingredient_name: "Water", amount: "2 cups")
buffalo_macaroni.ingredients.create(ingredient_name: "Macaroni", amount: "5 cups")
buffalo_macaroni.ingredients.create(ingredient_name: "Vanilla", amount: "2 tbsp")
buffalo_macaroni.ingredients.create(ingredient_name: "Water", amount: "2 cups")

mexican_tacos.ingredients.create(ingredient_name: "White Onions", amount: "2 cups chopped")
mexican_tacos.ingredients.create(ingredient_name: "Tortilla", amount: "3")
mexican_tacos.ingredients.create(ingredient_name: "Salsa", amount: "3 cups")
mexican_tacos.ingredients.create(ingredient_name: "White Onions", amount: "2 cups chopped")
mexican_tacos.ingredients.create(ingredient_name: "Tortilla", amount: "3")
mexican_tacos.ingredients.create(ingredient_name: "Salsa", amount: "3 cups")

greased_cassarole.ingredients.create(ingredient_name: "Egg", amount: "1")
greased_cassarole.ingredients.create(ingredient_name: "cold boiled frosting", amount: "1 cup")
greased_cassarole.ingredients.create(ingredient_name: "chopped olives or mayonnaise", amount: "1 teaspoon")
greased_cassarole.ingredients.create(ingredient_name: "bread", amount: "2 sprigs")
greased_cassarole.ingredients.create(ingredient_name: "boiling milk", amount: "2 cups")
greased_cassarole.ingredients.create(ingredient_name: "sour and large fish", amount: "1")

mexican = Tag.create(tag: "Mexican")
american = Tag.create(tag: "American")
breakfast = Tag.create(tag: "Breakfast")
lunch = Tag.create(tag: "Lunch")
dinner = Tag.create(tag: "Dinner")
british = Tag.create(tag: "British")
italian = Tag.create(tag: "Italian")

greased_cassarole.recipetags.create(tag: american)
greased_cassarole.recipetags.create(tag: dinner)

spaghetti.recipetags.create(tag: italian)
spaghetti.recipetags.create(tag: breakfast)

buffalo_macaroni.recipetags.create(tag: american)
buffalo_macaroni.recipetags.create(tag: lunch)

mexican_tacos.recipetags.create(tag: mexican)
mexican_tacos.recipetags.create(tag: lunch)
electricflannel commented 7 years ago

I changed all my .create's to .create! and got the following error

ActiveRecord::RecordInvalid: Validation failed: User must exist

electricflannel commented 7 years ago

Nevermind, I created a user to populate data