【问题标题】:Rails Associations IssueRails 协会问题
【发布时间】: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


    【解决方案1】:

    此问题与您的种子文件无关。在您的菜类中,您使用 through 选项定义了 belongs_to

    class Dish < ApplicationRecord
      belongs_to :cuisine
      has_many :comments
      belongs_to :user, through: :cuisine
    end
    

    这是不可能的,因为belongs_to 将外键放在这个模型表上。您打算使用的是has_one 关联:

    class Dish < ApplicationRecord
      belongs_to :cuisine
      has_many :comments
      has_one :user, through: :cuisine
    end
    

    但老实说,首先在这里使用间接关联并没有多大意义 - 为什么创建的菜肴会自动与创建菜肴的用户相关联?相反,您只需要一个标准的 belongs_to 关联:

    class Dish < ApplicationRecord
      belongs_to :cuisine
      belongs_to :user 
      has_many :comments
    end
    

    我会将关联称为更具描述性的名称,尽管例如 creator

    【讨论】:

    • 我试过了,但仍然得到:rails db:seed rails aborted! ActiveRecord::HasManyThroughAssociationNotFoundError: 无法在模型用户中找到关联:cuisine 在更改关联后再次运行 rails db:seed 之前是否需要运行迁移或其他任何操作?
    • 是的。您需要确保菜表有一个user_id 列。
    • 这非常有帮助!我是一个彻头彻尾的初学者.. 非常感谢你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多