创建迁移期间的Rails问题

时间:2015-09-17 09:53:40

标签: ruby-on-rails ruby ruby-on-rails-4

在我的ruby on rails应用程序中,我在终端中运行了这两个命令:

rails generate scaffold Order name address:text email pay_type

rails generate migration add_order_to_line_item order:references

我查看了rails

中db文件夹中的'schema.rb'文件

这是输出:

ActiveRecord::Schema.define(version: 20150917094441) do

  create_table "carts", force: :cascade do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
  end

# Could not dump table "line_items" because of following NoMethodError
#   undefined method `[]' for nil:NilClass

  create_table "orders", force: :cascade do |t|
    t.string   "name"
    t.text     "address"
    t.string   "email"
    t.string   "pay_type"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "products", force: :cascade do |t|
    t.string   "title"
    t.text     "description"
    t.string   "image_url"
    t.decimal  "price",       precision: 8, scale: 2
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
  end

end

表“line_item”无法转储。这些是迁移文件:

    class CreateOrders < ActiveRecord::Migration
  def change
    create_table :orders do |t|
      t.string :name
      t.text :address
      t.string :email
      t.string :pay_type

      t.timestamps null: false
    end
  end
end

class AddOrderToLineItem < ActiveRecord::Migration
  def change
    add_column :line_items, :order, :refernces
  end
end

我甚至无法执行rake db:rollback,因为我发现了这个错误:

  

未定义的方法to_sym' for nil:NilClass/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/schema_definitions.rb:258:in列'

我真的不知道我能做什么

2 个答案:

答案 0 :(得分:2)

它应该是:reference而不是:refernces

你可以尝试

class AddOrderToLineItem < ActiveRecord::Migration
  def change
    add_reference :line_items, :order, index: true
  end
end

答案 1 :(得分:1)

更改迁移,您可以在迁移中尝试此操作。

add_reference :line_items, :order