Ruby on rails 命名错误:未定义的方法` title';对于#<;成分:0x0000000005e6cf30>;

Ruby on rails 命名错误:未定义的方法` title';对于#<;成分:0x0000000005e6cf30>;,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,这个星期我已经遇到过几次这个错误,但这次我不知道是什么错了。我有一个名为配料的ActiveRecord模型 class CreateIngredients < ActiveRecord::Migration[5.2] def change create_table :ingredients do |t| t.string :title , null: false t.integer :availability t.decimal :pric

这个星期我已经遇到过几次这个错误,但这次我不知道是什么错了。我有一个名为
配料的ActiveRecord模型

class CreateIngredients < ActiveRecord::Migration[5.2]
  def change
    create_table :ingredients do |t|

      t.string :title , null: false
      t.integer :availability
      t.decimal :price, precision:15, scale: 2

      t.timestamps
    end
  end
end

有人能帮我理解我做错了什么吗?

如果没有添加此迁移,请尝试
rails db:migrate
,然后使用
rails控制台


当您第一次创建表时,您可能做了rails db:migrate,但是在添加了未添加的列之后,当您开始创建配料时,它知道表是什么,因此,为什么它不能超过标题。

您可以试试配料。新建并检查是否显示了“标题”列吗?您应该从
rails控制台
而不是从IRB尝试此操作。通过
cd/path/to/railsapp
-->转到rails应用程序,然后执行
rails控制台
,然后显示您的列表
schema.rb
@VigneshwaranSivalingam我得到了“NameError:未初始化常量成分”。你知道为什么吗?
class Ingredient < ApplicationRecord
  validates :title, presence: true

  has_many :ingredient_categories
  has_many :categories, through: :ingredient_categories
end
irb(main):003:0> Ingredient.create!(title: 'Cheese Spread')
#=> ActiveModel::UnknownAttributeError: unknown attribute 'title' for Ingredient.