Rails 3 => Undefined method 'array' when I try to use rab db: migrate

This is my first post here, so it's easy. I am trying to create my first application in Rails 3.2.1. I am trying to create 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 entry:

  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 can’t understand for life why this is so. But this is because I do not know what I am doing. Any advice / help would be greatly appreciated.

+4
source share
3 answers

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 the array.

 class Paint < ActiveRecord::Base serialize :paint_hex end 

Simple: Prefixing all of your attribute names with paint_ is a rather unusual naming scheme for a rails application.

+10
source

In Rails 4 and using PostgreSQL, you really can use an array type in the database:

Migration:

 class CreateSomething < ActiveRecord::Migration def change create_table :something do |t| t.string :some_array, array: true, default: [] t.timestamps end end end 
+6
source

An array of its invalid database type. You cannot create a column with an array of types.

There are several ways to store an array in a field. Check out the serialize method. You must declare a column of type text , and in the class indicate that the columns are serialized as an object of type array

+2
source

All Articles