7

This is my first post here so go easy. I am trying to build my first app in Rails 3.2.1. I am trying to generate a scaffold for Paint using the following terminal command:

rails generate scaffold Paint paint_family:string paint_hex:array paint_available:boolean     paint_location:integer paint_quantity:integer paint_additional_info:text

But when I try to migrate I get the following error:

undefined method `array' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x007fbd8bdb1c58>

Here is the migration record:

  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

end

I cannot for the life of me figure out why this is. But that's because I don't know what I'm doing. Any advice/help would be greatly appreciated.

ErikAtLarge
  • 83
  • 1
  • 7

3 Answers3

12

The problem is this:

t.array :paint_hex

There is no column type called array. You can use string or text and then serialize the value if you really want to save an array.

class Paint < ActiveRecord::Base
  serialize :paint_hex
end

Just by the way: Prefixing all your attribute names with paint_ is a pretty uncommon naming scheme for a rails application.

iblue
  • 29,609
  • 19
  • 89
  • 128
  • Then give me an upvote and select my answer as the correct one. That's how we show appreciation on stackoverflow... – iblue Jun 29 '12 at 19:17
10

In Rails 4 and using PostgreSQL you can actually use an array type in the DB:

Migration:

class CreateSomething < ActiveRecord::Migration
  def change
    create_table :something do |t|
      t.string :some_array, array: true, default: []
      t.timestamps
    end
  end
end
Adam Waite
  • 19,175
  • 22
  • 126
  • 148
2

Array its not a valid database type. You cant create a column with type array.

There is some ways to store an array in a field. Check the serialize method. You must declare the column of type text and the in the class specify that the columns serializas an object of type array

miguel.camba
  • 1,777
  • 1
  • 14
  • 19