将列添加到模型时的Rails语法错误

时间:2019-06-05 09:29:57

标签: ruby-on-rails ruby

我正在尝试使用遇到错误的rails构建音乐应用程序,只是想知道有人知道这意味着什么吗?

当我这样做

rails g migration add_quantity_to_line_items quantity:integer, default: 1
rails db:migrate

后退

rails aborted!
SyntaxError: /Users/Code/Ruby/musicapp/db/migrate/20190605091214_add_quantity_to_line_items.rb:4: syntax error, unexpected tSYMBEG, expecting do or '{' or '('
    add_column :line_items, :default, :string
               ^
/Users/mrswordsmith/Code/Ruby/musicapp/db/migrate/20190605091214_add_quantity_to_line_items.rb:5: syntax error, unexpected tINTEGER, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
    add_column :line_items, :1, :string
class AddQuantityToLineItems < ActiveRecord::Migration[5.2]
  def change
    add_column :line_items, :quantity, :integer,
    add_column :line_items, :default, :string
    add_column :line_items, :1, :string
  end
end

1 个答案:

答案 0 :(得分:3)

如果我正确理解您想要实现的目标是:

class AddQuantityToLineItems < ActiveRecord::Migration[5.2]
  def change
    add_column :line_items, :quantity, :integer, default: 1
  end
end

您的迁移脚本中有2个错误:

  • 您不能使用逗号(,
  • 默认迁移生成器不处理默认值,您必须手动更新迁移。

因此,迁移脚本仅应为rails g migration add_quantity_to_line_items quantity:integer