Forum: Ruby on Rails Migration Problem - wrong number of arguments

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E700f28e33f22aa356f21833cfd7cba4?d=identicon&s=25 Doug Meharry (doug3)
on 2007-02-13 22:38
Greetings all, I've been having trouble getting migration to work as I
understand it should.

Basically what I am doing is following the example (sort of) for making
a table that will store data that will be used to create a tree using
"acts_as_tree: -- shown on page 104 of the O'reily Rails Cookbook.

When I run "rake db:migrate" the code below successfully creates a table
named "tabs" as required -- so I know I have a good connection to the
database with necessary permissions.  However, the rake task throws an
error when it gets to the part where I want it to add some initial
default data to the table -- or at least I think this is where the
problem is.

Below is part of the trace and error message thrown by rake.  You'll
note the last line references: "001_create_database.rb:15" -- which is
the same line number where my migration code trys to add records to the
table.

C:\rails\tabs>rake db:migrate --trace
(in C:/rails/tabs)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
ActionController called
** Execute db:migrate
== CreateDatabase: migrating
==================================================
-- create_table(:tabs, {:force=>true})
   -> 0.1720s
rake aborted!
wrong number of arguments (1 for 0)
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:449:in
`initialize'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:449:in
`new'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:449:in
`create'
./db/migrate//001_create_database.rb:15:in `real_up'


Here is my code:

class CreateDatabase < ActiveRecord::Migration

  def self.up
    # Create a Tabs table
    create_table :tabs, :force => true do |t|
            t.column :parent_id, :integer
      t.column :name, :string, :limit => 36
      t.column :c_controller, :string
      t.column :c_action, :string
      t.column :c_id, :string
    end

    # Insert default data into the Tabs table for testing and
development
    Tab.create :parent_id => 0, :name => 'Home', :c_controller  =>
'page', :c_action => 'view', :c_id => '0'
    Tab.create :parent_id => 0, :name => 'About', :c_controller =>
'page', :c_action => 'view', :c_id => '1'
    Tab.create :parent_id => 2, :name => 'Team', :c_controller =>
'page', :c_action => 'view', :c_id => '2'
    Tab.create :parent_id => 2, :name => 'Contact', :c_controller =>
'page', :c_action => 'view', :c_id => '3'
    Tab.create :parent_id => 0, :name => 'Products', :c_controller =>
'page', :c_action => 'view', :c_id => '4'

  end

  def self.down
      drop_table :tabs
     end

end
B09a3f6cdc4797532647d2d264b5df49?d=identicon&s=25 Jodi Showers (jshow)
on 2007-02-13 22:50
(Received via mailing list)
On 13-Feb-07, at 4:38 PM, Doug Meharry wrote:

>       t.column :c_controller, :string
>       t.column :c_action, :string
>       t.column :c_id, :string
>     end
>
>     # Insert default data into the Tabs table for testing and
> development
>     Tab.create :parent_id => 0, :name => 'Home', :c_controller  =>
> 'page', :c_action => 'view', :c_id => '0'

Doug, it's my understanding that rails needs to be prodded to
understand your new model definition.

Prodding can be done like this:

class CreateDatabase < ActiveRecord::Migration
   class Tab < ActiveRecord::Base; end

#do your create table as usual, then..

Tab.reset_column_information #tell the model to check out it's
current/new definition

#then do your Tab.create statements

I use the above technique, and it works like a charm.

cheers,
Jodi
E700f28e33f22aa356f21833cfd7cba4?d=identicon&s=25 Doug Meharry (doug3)
on 2007-02-13 23:05
Jodi Showers wrote:
> On 13-Feb-07, at 4:38 PM, Doug Meharry wrote:
>
>>       t.column :c_controller, :string
>>       t.column :c_action, :string
>>       t.column :c_id, :string
>>     end
>>
>>     # Insert default data into the Tabs table for testing and
>> development
>>     Tab.create :parent_id => 0, :name => 'Home', :c_controller  =>
>> 'page', :c_action => 'view', :c_id => '0'
>
> Doug, it's my understanding that rails needs to be prodded to
> understand your new model definition.
>
> Prodding can be done like this:
>
> class CreateDatabase < ActiveRecord::Migration
>    class Tab < ActiveRecord::Base; end
>
> #do your create table as usual, then..
>
> Tab.reset_column_information #tell the model to check out it's
> current/new definition
>
> #then do your Tab.create statements
>
> I use the above technique, and it works like a charm.
>
> cheers,
> Jodi


Jodi -- first, thanks for the quick reply!  Your instructions worked
like a champ.  I printed it out and put a copy in my rails book.

Doug
This topic is locked and can not be replied to.