当我尝试rake db:migrate时,Rails 3 =>未定义的方法'array'

Eri*_*rge 6 arrays scaffold dbmigrate ruby-on-rails-3

这是我在这里发表的第一篇文章.我正在尝试在Rails 3.2.1中构建我的第一个应用程序.我正在尝试使用以下终端命令为Paint生成一个脚手架:

rails generate scaffold Paint paint_family:string paint_hex:array paint_available:boolean     paint_location:integer paint_quantity:integer paint_additional_info:text
Run Code Online (Sandbox Code Playgroud)

但是当我尝试迁移时,我收到以下错误:

undefined method `array' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x007fbd8bdb1c58>
Run Code Online (Sandbox Code Playgroud)

这是迁移记录:

  class CreatePaints < ActiveRecord::Migration
  def change
    create_table :paints do |t|
    t.string :paint_family
    t.array :paint_hex
    t.boolean :paint_available
    t.integer :paint_location
    t.integer :paint_quantity
    t.text :paint_additional_info

    t.timestamps
 end
 end
Run Code Online (Sandbox Code Playgroud)

结束

我无法为我的生活找出原因.但那是因为我不知道自己在做什么.任何建议/帮助将不胜感激.

ibl*_*lue 12

问题是这样的:

t.array :paint_hex
Run Code Online (Sandbox Code Playgroud)

没有调用列类型array.如果您确实要保存数组,可以使用stringtext然后序列化该值.

class Paint < ActiveRecord::Base
  serialize :paint_hex
end
Run Code Online (Sandbox Code Playgroud)

顺便说一句:paint_为rails应用程序添加前缀是一个非常不常见的命名方案.

  • 非常感激.并感谢关于'paint_'的说明. (2认同)

Ada*_*ite 8

在Rails 4中并使用PostgreSQL,您实际上可以在DB中使用数组类型:

移民:

class CreateSomething < ActiveRecord::Migration
  def change
    create_table :something do |t|
      t.string :some_array, array: true, default: []
      t.timestamps
    end
  end
end
Run Code Online (Sandbox Code Playgroud)