gpt4 book ai didi

ruby-on-rails - ActiveRecord::SubclassNotFound:单表继承机制未能定位子类

转载 作者:行者123 更新时间:2023-12-03 06:29:15 25 4
gpt4 key购买 nike

我是 Rails 新手。
我有两个模型类别产品,如下所示:-

class Category < ActiveRecord::Base
attr_accessible :type

has_many :products
end

class Product < ActiveRecord::Base
attr_accessible :category_id, :color, :price, :title

belongs_to :category
end

我的schema.rb如下:-

ActiveRecord::Schema.define(:version => 20130725220046) do

create_table "categories", :force => true do |t|
t.string "type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "products", :force => true do |t|
t.integer "category_id"
t.decimal "price", :precision => 10, :scale => 0
t.string "title"
t.string "color"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

end

在 Rails 控制台中,我使用 Product.create 命令创建了两个产品,其中包含两个产品

[#<Product id: 1, category_id: 1, price: 500, title: "shirt", color: "blue", `created_at: "2013-07-25 22:04:54", updated_at: "2013-07-25 22:04:54">, #<Product id: 2, category_id: 1, price: 600, title: "tees", color: "black", created_at: "2013-07-25 22:05:17", updated_at: "2013-07-25 22:05:17">]`  

并使用控制台中的Category.create命令创建了两个类别

<Category id: 1, type: "clothing", created_at: "2013-07-25 22:03:54", updated_at: "2013-07-25 22:03:54"><Category id: 2, type: "footwear", created_at: "2013-07-25 22:04:02", updated_at: "2013-07-25 22:04:02">  

现在,Product.all 工作正常,但 Category.all 给出

ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'clothing'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Category.inheritance_column to use another column for that information.

这里面有什么问题吗?我想在类别和产品之间建立关系,例如
一个类别有_许多产品,并且产品属于_一个类别。

最佳答案

type 是受限词,您不能将其用作 ActiveRecord 模型中的列名称(除非您正在执行 STI)。

关于ruby-on-rails - ActiveRecord::SubclassNotFound:单表继承机制未能定位子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17879024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com