Rails关联has_many通过:未初始化的常量错误应该很容易回答:是初学者

问题描述 投票:0回答:1

模型BarberShop (table barber_shops)Barber (table barbers)都存在。在rails c之后

x = Barber.new and x.include? :barbershop => true. 

因此关联应正确设置。当我尝试保存时,会出现

名称错误,未初始化的常量BarberShop :: Barber

我知道我很难找到麻烦。例如,我今天用has-many-through-association了解到关联的顺序(在模型中)很重要,必须列出a才能获得b-即

has_many :a 
has_many :b, through: :a

型号如下-

Barber < ApplicationRecord
  belongs_to :barbershop
  has_many :appointments 
  has_many :customers, through: :appointments


BarberShop < ApplicationRecord 
    has_many :barbers
end 
#schema in db
create_table "barber_shops", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "barbers", force: :cascade do |t|
    t.string "name"
    t.string "email"
    t.integer "barbershop_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["barbershop_id"], name: "index_barbers_on_barbershop_id"
  end

ruby-on-rails activerecord
1个回答
0
投票

在Rails应用中,模型BarberShop必须位于文件app/models/barber_shop.rb中,而模型Barber必须位于文件app/model/barber.rb中。

如果您没有正确命名文件,则可能会出现奇怪的行为,因为对象不会自动加载。

© www.soinside.com 2019 - 2024. All rights reserved.