- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试在Rails应用程序中创建新产品时出现此错误。
Invalid single-table inheritance type: Movie is not a subclass of Product
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
@products = Product.all
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
@product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render action: 'show', status: :created, location: @product }
else
format.html { render action: 'new' }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:title, :artist, :type, :release_date, :category, :publisher, :format, :description, :sale_price, :rental_price)
end
end
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :title
t.string :artist
t.string :type
t.date :release_date
t.string :category
t.string :publisher
t.string :format
t.text :description
t.decimal :sale_price
t.decimal :rental_price
t.timestamps
end
end
end
最佳答案
不应将type
关键字作为列名,因为是ActiveRecord 的保留字。
但是,如果出于任何原因(例如,如果您无法控制数据库结构),您确实要使用它,则应该执行以下操作:
首先,确保您的Movie模型继承自(false-)“抽象”模型产品:
class Product < ActiveRecord::Base
TYPES = %w( Movie )
before_save :set_type
validates :type, presence: true, :inclusion => { :in => TYPES }
def set_type
raiser "You must override this method in each model inheriting from Product!"
end
# ...
class Movie < Product
def set_type # If you don't implement this method, an error will be raised
self.type = 'Movie'
end
class Book < Product
def set_type
self.type = 'Book'
end
#...
class Product < ActiveRecord::Base
TYPES = %w( Movie Book )
关于ruby-on-rails - 无效的单表继承类型:Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20357997/
我习惯于创建基本查询,所以我被困在一个相当复杂的查询上。 Order Number Order Line Package Part Number Size Cost
我想创建类似 tinder/whatsapp 的消息。 我有一个名为“消息”的表,我想显示用户之间的所有对话(最后一条消息) 表结构: message_id | user_id | recipient
我有一个如下所示的表,我想获取每个 loc_id 的最新 insp_date 的 ID (insp_id)。 create table insp ( insp_id int (10),
我正在尝试解决以下问题,如 this table, sql statement and comments 所示 表列:id,版本,idx_on; PK 是“id”列 所以,我从子查询中得到一组元组{i
我正在尝试使用 HQL 创建简单的选择查询。有一个将在查询中使用的实体。它看起来像下面这样: @Entity @Table(name = "my_table") public class MyTabl
我有一个带有单表映射的层次结构。我使用 HSQLDB 进行自动生成 DDL 功能的测试。层次结构中的子类之一具有标量长字段。 我无法保留其他子类的对象,因为第一个类的数据库行字段为空。 Hiberna
我有不同的客户及其信息。所有客户都有相同的信息。一个客户端就是一张表,每个客户端都具有与其他客户端相同的列。 我的问题是,什么更有效或什么更好,或者哪种是实现此目的的最佳方法? 只有一张表包含所有客户
我目前正在开发一个系统,允许使用关键字标记文档并具有以下形式的表格: CREATE TABLE `KeywordsToDocuments` ( `keywordID` int NOT NULL
我有一个成员表:成员(id, name, gender, head_id) 我想要的是建立以户主(家庭成员)为基础的家庭关系。就像这样:一个成员属于一个家庭(由成员定义) 也许我可以将其拆分为 2 个
我有下表: id | billingno | location ------------------------- 1 | 9999999 | Toronto 2 | 9999999 |
我被要求为我们的应用程序考虑一个数据库。大约有7种不同的数据需要存储。一种是标识数据,其中可能包含唯一的序列号、时间、位置。所有其他 6 个数据集(4 个二进制原始数据,2 个文本数据)必须由标识数据
我在 stackoverflow 上搜索过类似的问题,但我不明白如何让它工作,我想做什么... 所以,我想从数据库中获取最近 7 天的交易并获取总销售额,如果某天没有数据,还包括空行。 我目前拥有的:
想象一下以下类别:酒吧、吃饭的地方、商店等...... 每一个分类都会有一些共同的字段,比如 id, name, address and geolocation (Lat and Lng positi
我一直在搜索整个谷歌 - 我可能只是愚蠢地理解答案。 我在 mysql 中有一个表 - 问题。 +-------------+---------------+------+-----+--------
我是 SQL 和 mySQL 的新手,我自学并尝试编写一个查询,向我显示 ID 列中匹配的 ID 的计数为 3 或更少的所有行。 例如: Id timestamp Num
我目前正在处理一个查询,该查询根据书的属性从表中搜索书籍。该表包含超过 5000 万行,结构如下: ----------------------- | book_id | attr_id | --
我是一名优秀的程序员,十分优秀!