Demo in rails2.pdf of add_price migration, PostgreSQL 8.1.4

People,

I’m looking at the migrate example on p 74 of the rails2.pdf

It’s a simple example of using a migration to add a column to
the products table.

Here is a copy of the migration file named 002_add_price.rb
after I generated and then edited it:

class AddPrice < ActiveRecord::Migration
def self.up
add_column :products, :price, :decimal, :precision => 8, :scale =>
2,
:default => 0
end

def self.down
remove_column :products, :price
end
end

I see this error when I walk through the demo:

bash mac maco /pt/books/rails2_my_depot/depot 9 $
bash mac maco /pt/books/rails2_my_depot/depot 9 $ rake db:migrate
–trace
(in /pt/books/rails2_my_depot/depot)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
== AddPrice: migrating

– add_column(:products, :price, :decimal, {:precision=>8, :scale=>2,
:default=>0})
rake aborted!
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]
/r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:259:in
type_to_sql' /r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/postgresql_adapter.rb:310:inadd_column’
/r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/migration.rb:272:in
method_missing' /r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/migration.rb:256:insay_with_time’
/r/lib/ruby/1.8/benchmark.rb:293:in measure' /r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/migration.rb:256:insay_with_time’
/r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/migration.rb:270:in
method_missing' ./db/migrate//002_add_price.rb:3:inreal_up’
/r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/migration.rb:210:in
migrate' /r/lib/ruby/1.8/benchmark.rb:293:inmeasure’
/r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/migration.rb:210:in
migrate' /r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/migration.rb:332:inmigrate’
/r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/migration.rb:327:in
migrate' /r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/migration.rb:294:inup’
/r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/migration.rb:285:in
migrate' /r/lib/ruby/gems/1.8/gems/rails-1.1.4/lib/tasks/databases.rake:4 /r/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:387:inexecute’
/r/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:387:in execute' /r/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:357:ininvoke’
/r/lib/ruby/1.8/thread.rb:135:in synchronize' /r/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:350:ininvoke’
/r/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:1906:in run' /r/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:1906:inrun’
/r/lib/ruby/gems/1.8/gems/rake-0.7.1/bin/rake:7
/r/bin/rake:18
bash mac maco /pt/books/rails2_my_depot/depot 10 $
bash mac maco /pt/books/rails2_my_depot/depot 10 $
bash mac maco /pt/books/rails2_my_depot/depot 10 $

Info about my setup:

bash mac maco /pt/books/rails2_my_depot/depot 12 $
bash mac maco /pt/books/rails2_my_depot/depot 12 $
bash mac maco /pt/books/rails2_my_depot/depot 12 $ uname -a
Darwin mac 8.7.0 Darwin Kernel Version 8.7.0: Fri May 26 15:20:53 PDT
2006;
root:xnu-792.6.76.obj~1/RELEASE_PPC Power Macintosh powerpc
bash mac maco /pt/books/rails2_my_depot/depot 13 $
bash mac maco /pt/books/rails2_my_depot/depot 13 $
bash mac maco /pt/books/rails2_my_depot/depot 13 $
[ os x 10.4 ]

bash mac maco /pt/books/rails2_my_depot/depot 19 $
bash mac maco /pt/books/rails2_my_depot/depot 19 $
bash mac maco /pt/books/rails2_my_depot/depot 19 $
bash mac maco /pt/books/rails2_my_depot/depot 19 $ psql
depot_development
Welcome to psql 8.1.4, the PostgreSQL interactive terminal.

bash mac maco /pt/books/rails2_my_depot/depot 12 $
bash mac maco /pt/books/rails2_my_depot/depot 12 $
bash mac maco /pt/books/rails2_my_depot/depot 12 $
bash mac maco /pt/books/rails2_my_depot/depot 12 $
bash mac maco /pt/books/rails2_my_depot/depot 11 $ gem list --local

*** LOCAL GEMS ***

actionmailer (1.2.3)
Service layer for easy email delivery and testing.

actionpack (1.12.3)
Web-flow and rendering framework putting the VC in MVC.

actionwebservice (1.1.4)
Web service support for Action Pack.

activerecord (1.14.3)
Implements the ActiveRecord pattern for ORM.

activesupport (1.3.1)
Support and utility classes used by the Rails framework.

postgres (0.7.1)
The extension library to access a PostgreSQL database from Ruby.

rails (1.1.4)
Web-application framework with template engine, control-flow layer,
and ORM.

rake (0.7.1)
Ruby based make-like utility.

sources (0.0.1)
This package provides download sources for remote gem installation
bash mac maco /pt/books/rails2_my_depot/depot 12 $
bash mac maco /pt/books/rails2_my_depot/depot 12 $
bash mac maco /pt/books/rails2_my_depot/depot 12 $
bash mac maco /pt/books/rails2_my_depot/depot 12 $

Anyone have any clues on how I can get this to work?

Thanks,

-Dan

Dan,

My impression from previous posts and Dave T.'s comments when the
new version fo the beta book came out (haven’t tried it all myself) is
that the :decimal column in migrations, linked to the ruby BigDecimal
class, is a feature currently in Edge Rails, but not in Rails 1.1.4.
So, you can either wait for 1.2 or whenever it shows up in a release, or
freeze edge rails in your app.

Asa

Dan B. wrote:

People,

I’m looking at the migrate example on p 74 of the rails2.pdf

It’s a simple example of using a migration to add a column to
the products table.

Here is a copy of the migration file named 002_add_price.rb
after I generated and then edited it:

class AddPrice < ActiveRecord::Migration
def self.up
add_column :products, :price, :decimal, :precision => 8, :scale =>
2,
:default => 0
end

def self.down
remove_column :products, :price
end
end

I see this error when I walk through the demo:

bash mac maco /pt/books/rails2_my_depot/depot 9 $
bash mac maco /pt/books/rails2_my_depot/depot 9 $ rake db:migrate
–trace
(in /pt/books/rails2_my_depot/depot)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
== AddPrice: migrating

– add_column(:products, :price, :decimal, {:precision=>8, :scale=>2,
:default=>0})
rake aborted!
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]
/r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:259:in
type_to_sql' /r/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/postgresql_adapter.rb:310:inadd_column’

–snip–

bash mac maco /pt/books/rails2_my_depot/depot 12 $
bash mac maco /pt/books/rails2_my_depot/depot 12 $
bash mac maco /pt/books/rails2_my_depot/depot 11 $ gem list --local

*** LOCAL GEMS ***

rails (1.1.4)
Web-application framework with template engine, control-flow layer,
and ORM.

Well,

I ran a simple experiment where I tried adding a column with data type
of
string
rather than decimal and it worked well.

Then, I ran another experiment where I tried creating a table with a
column
with
data type of decimal and it succeeded:

bash mac maco /pt/books/rails2_my_depot/depot 18 $
bash mac maco /pt/books/rails2_my_depot/depot 18 $
bash mac maco /pt/books/rails2_my_depot/depot 18 $ script/generate model
testdecimal
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/testdecimal.rb
create test/unit/testdecimal_test.rb
create test/fixtures/testdecimals.yml
exists db/migrate
create db/migrate/003_create_testdecimals.rb
bash mac maco /pt/books/rails2_my_depot/depot 19 $
bash mac maco /pt/books/rails2_my_depot/depot 19 $ vi
db/migrate/003_create_testdecimals.rb

bash mac maco /pt/books/rails2_my_depot/depot 20 $
bash mac maco /pt/books/rails2_my_depot/depot 20 $ cat
db/migrate/003_create_testdecimals.rb
class CreateTestdecimals < ActiveRecord::Migration
def self.up
create_table :testdecimals do |t|
t.column :dec1, :decimal
end
end

def self.down
drop_table :testdecimals
end
end
bash mac maco /pt/books/rails2_my_depot/depot 21 $
bash mac maco /pt/books/rails2_my_depot/depot 21 $
bash mac maco /pt/books/rails2_my_depot/depot 21 $

bash mac maco /pt/books/rails2_my_depot/depot 21 $
bash mac maco /pt/books/rails2_my_depot/depot 21 $
bash mac maco /pt/books/rails2_my_depot/depot 21 $ rake db:migrate
(in /pt/books/rails2_my_depot/depot)
== CreateTestdecimals: migrating

– create_table(:testdecimals)
NOTICE: CREATE TABLE will create implicit sequence
“testdecimals_id_seq”
for serial column “testdecimals.id”
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
“testdecimals_pkey” for table “testdecimals”
-> 0.2935s
== CreateTestdecimals: migrated (0.2939s)

bash mac maco /pt/books/rails2_my_depot/depot 22 $
bash mac maco /pt/books/rails2_my_depot/depot 22 $

depot_development=#
depot_development=#
depot_development=#
depot_development=# \d testdecimals
\d testdecimals
Table “public.testdecimals”
Column | Type |
Modifiers
--------±--------±----------------------------------------------------------
id | integer | not null default
nextval(‘testdecimals_id_seq’::regclass)
dec1 | numeric |
Indexes:
“testdecimals_pkey” PRIMARY KEY, btree (id)

depot_development=#
depot_development=#
depot_development=#

So, it looks like my problem is isolated to the situation where
I want to add a column with data type of decimal to a table.

The migration works okay if the decimal data type resides in a
create_table() method.

-Dan

Nope,

I’m not using BigDecimal nor edge rails.

I used gem to install rails on July 29 so right now my rails is at
rails-1.1.4

-Dan

On 8/3/06, Dan B. [email protected] wrote:

Well,

I ran a simple experiment where I tried adding a column with data type of
string rather than decimal and it worked well.

Then, I ran another experiment where I tried creating a table with a column
with data type of decimal and it succeeded:

is that migration (the latest revision of Agile Web…) using
BigDecimal (as seen in edge rails)?

regards,
jean-pierre

On Aug 4, 2006, at 2:03 AM, Dan B. wrote:

I’m not using BigDecimal nor edge rails.

I used gem to install rails on July 29 so right now my rails is at
rails-1.1.4

You need to be running edge Rails in order to run these examples–the
book is written in the anticipation of 1.2, which support BigDecimal
for decimal columns. It’s a significant enough change that I wanted
to roll it in now.

Regards

Dave T.

On 8/4/06, Dan B. [email protected] wrote:

Nope,

I’m not using BigDecimal nor edge rails.

I used gem to install rails on July 29 so right now my rails is at
rails-1.1.4

precisely, i thought that the latest push of Agile Web… used the
BigDecimal support found in edge rails?

cheers,
jean-pierre