【发布时间】:2021-11-04 23:22:25
【问题描述】:
由于某种原因,我在播种数据时遇到问题。 我觉得我的模特协会出了点问题。
有人知道出了什么问题吗?
我有 4 个模型 - 如您所见, 我还附上了架构和错误。
感谢您的帮助!
模型
class User < ApplicationRecord
has_secure_password
has_many :cuisines
has_many :dishes, through: :cuisine
has_many :comments
end
class Dish < ApplicationRecord
belongs_to :cuisine
has_many :comments
belongs_to :user, through: :cuisine
end
class Cuisine < ApplicationRecord
belongs_to :user
has_many :dishes
end
class Comment < ApplicationRecord
belongs_to :user
belongs_to :dish
end
**SCHEMA**
create_table "comments", force: :cascade do |t|
t.string "content"
t.integer "user_id"
t.integer "dish_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "cuisines", force: :cascade do |t|
t.string "country"
t.string "picture"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "user_id"
end
create_table "dishes", force: :cascade do |t|
t.string "name"
t.string "picture"
t.string "ingredients"
t.string "directions"
t.string "cook_time"
t.string "yield"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "cuisine_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "username"
t.string "password_digest"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end
**SEED FILE**
User.create(name: "Adi", username: "adi3", password: "password")
italian = Cuisine.create(country: "Italy", picture: "https://www.delonghi.com/Global/recipes/multifry/pizza_fresca.jpg", user_id:1)
pizza = italian.dishes.create(name: "Pizza", picture: "https://d1uz88p17r663j.cloudfront.net/original/4274048cd5f17c49dfee280f77a3739d_Cheese-Pizza_HB-2.jpg", ingredients: "flour, water, yeast, oil, salt, cheese, olives", directions: "mix everything together and add toppings", cook_time: "30 min", yield: "1 big pizza", cuisine_id:1)
adi = User.first
adi_pasta = adi.dishes.create(name: "Pomodoro Pasta", picture: "https://lh3.googleusercontent.com/_mkIjqOA29bzih8kz98RjBqf6KbLaan2ReyAzM2-Vj7SPDWGF24vPLz3zdTiwRdHaDdn6ed5kHdUUWkyoOQ83ZE=s640-c-rw-v1-e365", ingredients: "olive oil, garlic, basil, tomato, pasta", directions: "mix everything, cook pasta and top it up", cook_time: "20 min", yield: "4 portions", cuisine_id:1)
adi.comments.create(content: "Great easy tomato pasta", dish: adi_pasta, user_id:1, dish_id:2)
**ERROR**
// ☆♥☆ > rails db:seed 导轨中止! ArgumentError:未知键::通过。有效键是::class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :autosave, :required, :touch, :polymorphic, :counter_cache, :optional, :default /Users/username/project-backend/app/models/dish.rb:4:in `class:Dish'
【问题讨论】:
标签: ruby-on-rails associations has-many-through belongs-to