I do not know why this is breaking. I'm trying to run a migration on some code I've inherited and I've hit a snag. Here is the error message followed by the migration file code.
== AddStorecreditGizmoType: migrating ========================================
rake aborted!
NoMethodError: undefined method `find_by_name' for GizmoCategory(id: integer, description: string):Class
/var/lib/gems/1.9.1/gems/activerecord-2.3.14/lib/active_record/base.rb:1876:in `method_missing'
/home/thefonso/site-dev/vendor/plugins/will_paginate/lib/will_paginate/finder.rb:175:in `method_missing_with_paginate'
db/migrate//20090628000954_add_storecredit_gizmo_type.rb:4:in `up'
/var/lib/gems/1.9.1/gems/activerecord-2.3.14/lib/active_record/migration.rb:282:in `block in migrate'
/var/lib/gems/1.9.1/gems/activerecord-2.3.14/lib/active_record/migration.rb:282:in `migrate'
/var/lib/gems/1.9.1/gems/activerecord-2.3.14/lib/active_record/migration.rb:365:in `migrate'
/var/lib/gems/1.9.1/gems/activerecord-2.3.14/lib/active_record/migration.rb:457:in `run'
/var/lib/gems/1.9.1/gems/activerecord-2.3.14/lib/active_record/migration.rb:409:in `run'
/var/lib/gems/1.9.1/gems/rails-2.3.14/lib/tasks/databases.rake:135:in `block (3 levels) in <top (required)>'
/var/lib/gems/1.9.1/gems/rake-11.3.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:migrate:up
(See full trace by running task with --trace)
Here is the migration file code
class AddStorecreditGizmoType < ActiveRecord::Migration
def self.up
# TODO: GizmoCategory.find_by_name("misc") is breaking...why? Attempted to replace with "where" but same error.
new = GizmoType.new(:name => "store_credit", :description => "Store Credit", :gizmo_category => GizmoCategory.find_by_name("misc"), :required_fee_cents => 0, :suggested_fee_cents => 0)
new.save!
DB.execute("UPDATE gizmo_contexts_gizmo_types SET gizmo_type_id = #{new.id} WHERE gizmo_type_id IN (SELECT id FROM gizmo_types WHERE name = 'gift_cert');")
end
def self.down
DB.execute("UPDATE gizmo_contexts_gizmo_types SET gizmo_type_id = (SELECT id FROM gizmo_types WHERE name = 'gift_cert') WHERE gizmo_type_id IN (SELECT id FROM gizmo_types WHERE name = 'store_credit');")
GizmoType.find_by_name("store_credit").destroy
end
end
I've tried using "where" and "Find_by" but I keep getting this same "undefined method blahblah for GizmoCategory
Can you point me in the right direction? What am I missing? forgetting? What's happening here?
Oh and versions are as follows
rails - 2.3.14
ruby - 1.9.3p194
Thanks
The find_by_* methods didn't exist in activerecord 2.3.14. Use Model.find instead.
category = GizmoCategory.find(:first, conditions: "name = 'store_credit'")
category.destroy if category
Related
I'm trying to migrate the rails app but it fail. the issue in the paperclip gem migration.
I tried to use both way for migration but both of them not work properly:
class AddAttachmentImageToItems < ActiveRecord::Migration[6.1]
def self.up
add_attachment :items, :image
end
def self.down
remove_attachment :items, :image
end
end
also tried:
class AddAttachmentImageToItems < ActiveRecord::Migration[6.1]
def change
add_attachment :items, :image
end
end
after run rails db:migrate showing error:
/Users/zi/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/pry-byebug-3.8.0/lib/pry-byebug/control_d_handler.rb:5: warning: control_d_handler's arity of 2 parameters was deprecated (eval_string, pry_instance). Now it gets passed just 1 parameter (pry_instance)
== 20210713231704 AddAttachmentImageToItems: migrating ========================
-- add_attachment(:items, :image)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
wrong number of arguments (given 4, expected 3)
/Users/zi/rails/stage_1/db/migrate/20210713231704_add_attachment_image_to_items.rb:3:in `up'
/Users/zi/rails/stage_1/bin/rails:5:in `<top (required)>'
/Users/zi/rails/stage_1/bin/spring:10:in `block in <top (required)>'
/Users/zi/rails/stage_1/bin/spring:7:in `<top (required)>'
Caused by:
ArgumentError: wrong number of arguments (given 4, expected 3)
/Users/zi/rails/stage_1/db/migrate/20210713231704_add_attachment_image_to_items.rb:3:in `up'
/Users/zi/rails/stage_1/bin/rails:5:in `<top (required)>'
/Users/zi/rails/stage_1/bin/spring:10:in `block in <top (required)>'
/Users/zi/rails/stage_1/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Found the issue in the ruby version, downgrade to 2.6.8 and migration work properly.
I am working a new project in Codeanywhere (I am new on Ruby), but when I try to use "rake db : migrate" I have this error:
rake aborted!
Don't know how to build task ':' (see --tasks)
/home/cabox/.rvm/gems/ruby-2.1.2/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
(See full trace by running task with --trace)
Before this I use bundle update and after and nothing, and when I put "rake-T", db Migartion is on the tasks, when I try with --trace, pass the same.
I try before with rake db:migrate and the same happened
`rake aborted!
SyntaxError:
/home/cabox/workspace/blog/db/migrate/20170103233409_create_posts.rb:5: syntax error, unexpected ':'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:761:in `load_migration'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord- 4.1.6/lib/active_record/migration.rb:757:in `migration'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:752:in `disable_ddl_transaction'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:1044:in `use_transaction?'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:1036:in `ddl_transaction'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:990:in `execute_migration_in_transaction'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:952:in `block in migrate'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:948:in `each'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:948:in `migrate'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:807:in `up'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:785:in `migrate'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)` >
Does anyone know how to solve this problem?
In the migration file I have:
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.strind :
t.text :body
t.timestamps
end
end
end
I am pretty sure what is happening is you are inserting a space in there when you type it out. It needs to be
rake db:migrate
not
rake db : migrate
That error message is pretty common when there is a spelling error or some other kind of typing error when running the command.
Your migration has a typo as well, should be this
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.text :body
t.timestamps
end
end
end
Rake is a software task management tool, similar to Make, etc. in other systems
The generic syntax is
rake namesapce:task
In your case it is
rake db:migrate
Try doing rake db:reset if you want to rebuild your db again. It does 4 things for you
rake db:drop
rake db:create
rake db:migrate
rake db:seed
I'm trying to get a legacy Rails app up and running and when I do a
rake db:migrate
I get the following:
arx_rails [master●] % rake db:migrate
== AddSessionsTable: migrating ===============================================
-- create_table(:sessions)
rake aborted!
An error has occurred, all later migrations canceled:
Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead: CREATE TABLE `sessions` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `session_id` varchar(255) NOT NULL, `data` text, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/schema_statements.rb:170:in `create_table'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:434:in `create_table'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:466:in `block in method_missing'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:438:in `block in say_with_time'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:438:in `say_with_time'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:458:in `method_missing'
/Users/steve/projects/asu/arx_rails/db/migrate/20120825213542_add_sessions_table.rb:3:in `change'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:407:in `block (2 levels) in migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:407:in `block in migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:123:in `with_connection'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:389:in `migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:528:in `migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:777:in `call'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:777:in `ddl_transaction'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:719:in `block in migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:700:in `each'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:700:in `migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:570:in `up'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:551:in `migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:153:in `block (2 levels) in <top (required)>'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/bin/ruby_executable_hooks:15:in `eval'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/bin/ruby_executable_hooks:15:in `<main>'
The migration file is:
class AddSessionsTable < ActiveRecord::Migration
def change
create_table :sessions do |t|
t.string :session_id, :null => false
t.text :data
t.timestamps
end
add_index :sessions, :session_id
add_index :sessions, :updated_at
end
end
Rails has always created the id field itself without having to have it explicitly coded in the migration file and that it makes it a primary key but without a default value. So, I'm wondering where it might be setting the id fields to a DEFAULT of NULL for newly created tables. In some configuration file somewhere I don't know about?
Has anyone seen this before?
It looks like an issue with Rails and MySQL:
https://github.com/rails/rails/pull/13247
In the notes, user "pjg" suggests the following:
With Rails 2.3.5, MySQL version 5.7.9 and mysql gem you need to have
this bit as an initializer in
config/initializers/abstract_mysql_adapter.rb:
class ActiveRecord::ConnectionAdapters::MysqlAdapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
For mysql2 it should be
config/initializers/abstract_mysql2_adapter.rb:
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
Does that resolve the issue?
(EDIT: giving credit to solution provider)
For Rails 3.0.5 works that way:
class ActiveRecord::ConnectionAdapters::ColumnDefinition
def sql_type
type.to_sym == :primary_key ? 'int(11) auto_increment PRIMARY KEY' : base.type_to_sql(type.to_sym, limit, precision, scale) rescue type
end
end
With Rails 4.0.6 works fine:
config/initializers/abstract_mysql2_adapter.rb:
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) NOT NULL auto_increment PRIMARY KEY"
end
$ gem list mysql
* LOCAL GEMS *
mysql2 (0.3.18)
I just faced with problem when my migration with custom sql works fine on development environment and failed on production.
My migration looks like this:
class DbPatch < ActiveRecord::Migration
def up
execute("START TRANSACTION")
execute("update users set status = 1 where status = 3")
execute("COMMIT")
end
def down
execute("START TRANSACTION")
execute("update users set status = 3 where status = 1")
execute("COMMIT")
end
end
And on production env migration failed with next error message:
== 20140627155848 DbPatch: migrating =================================
-- execute("START TRANSACTION")
-> 0.0001s
-- execute("update users set status = 1 where status = 3")
-> 0.0003s
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
undefined method `-#' for nil:NilClass/var/www/app/releases/20140628083752/db/migrate/20140627155848_db_patch.rb:4:in `up'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:598:in `exec_migration'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:578:in `block in migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:577:in `migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:752:in `migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:994:in `block in execute_migration_in_transaction'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:1042:in `ddl_transaction'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:993:in `execute_migration_in_transaction'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:954:in `block in migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:950:in `each'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:950:in `migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:808:in `up'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:785:in `migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
After an half hour of research in ActiveRecord sources I recognized that problem was in my custom 'START TRANSACTION'.
It looks like rails automatically execute all migrations on production environment in transaction.
So the next code works correct:
class DbPatch < ActiveRecord::Migration
def up
execute("update users set status = 1 where status = 3")
end
def down
execute("update users set status = 3 where status = 1")
end
end
I'm posting this topic here to keep knowledges not only in my head :)
I am using rails 2.x version. Am using mysql 5.0 database for back end process. I faced a migration error when i did the following steps.
Get the application code from github.
Run schema:create, load and seed process
After that add some data in the application.
Again Pull the code from the same branch.
After that i run db:migrate it throws the following error
-> rake db:migrate --trace
DEPRECATION WARNING: Rake tasks in vendor/plugins/acts_as_audited/tasks,
vendor/plugins/annotate_models/tasks,
vendor/plugins/app_version/tasks, vendor/plugins/bullet/tasks,
vendor/plugins/importer/tasks, vendor/plugins/mimetype-fu/tasks,
vendor/plugins/railsdav/tasks, vendor/plugins/rav/tasks,
vendor/plugins/simple_captcha/tasks, vendor/plugins/smart_table/tasks,
vendor/plugins/test_data_generator/tasks,
vendor/plugins/visualize_models/tasks, and
vendor/plugins/xss_terminate/tasks are deprecated. Use lib/tasks
instead. (called from
/usr/lib/ruby/gems/1.8/gems/rails-2.3.12/lib/tasks/rails.rb:10)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environmentThu 11 Jul 2013 09:24:36 AM IST
** Execute db:migrate
== ConvertOrganizationToAwesomeNestedSet: migrating ==========================
Running sample patched rebuild process.
rake aborted!
An error has occurred, all later migrations canceled:
Validation failed: Name can't be blank
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/validations.rb:1102:in
save_without_dirty!'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/dirty.rb:87:in
save_without_transactions!'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:200:in
save!'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in
transaction'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:182:in
transaction'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:200:in
save!'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:208:in
rollback_active_record_state!'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:200:in
save!'
/opt/sample/lib/awesome_nested_set_overrides.rb:25:in rebuild!'
/opt/sample/lib/awesome_nested_set_overrides.rb:22:incall'
/opt/sample/lib/awesome_nested_set_overrides.rb:22:in rebuild!'
/opt/ssample/lib/awesome_nested_set_overrides.rb:22:ineach'
/opt/sample/lib/awesome_nested_set_overrides.rb:22:in rebuild!'
/opt/sample/lib/awesome_nested_set_overrides.rb:32:incall'
/opt/sample/lib/awesome_nested_set_overrides.rb:32:in rebuild!'
/opt/sample/lib/awesome_nested_set_overrides.rb:29:ineach'
/opt/sample/lib/awesome_nested_set_overrides.rb:29:in rebuild!'
./db/migrate//20130102220216_convert_organization_to_awesome_nested_set.rb:6:in
up_without_benchmarks'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:282:in
send'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:282:in
migrate'
/usr/lib/ruby/1.8/benchmark.rb:293:in measure'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:282:in
migrate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:365:in
__send__'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:365:in
migrate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:491:in
migrate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:567:in
call'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:567:in
ddl_transaction'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:490:in
migrate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:477:in
each'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:477:in
migrate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:401:in
up'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:383:in
migrate'
/usr/lib/ruby/gems/1.8/gems/rails-2.3.12/lib/tasks/databases.rake:112
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:228:in call'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:228:inexecute'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:223:in each'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:223:inexecute'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:166:in invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:insynchronize'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:159:in invoke_with_call_chain'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:152:ininvoke'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:143:in
invoke_task'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:101:in
top_level'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:101:in
each'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:101:in
top_level'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:110:in
run_with_threads'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:95:in
top_level'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:73:in
run'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:160:in
standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:70:in
run'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/bin/rake:33
/usr/bin/rake:19:inload'
/usr/bin/rake:19
Tasks: TOP => db:migrate
Organization.rebuild! - This is the code that calls the plugin.
It seems like there is some problem with awesome_nested_set plugin. I am struggle with this for past one week. Any comments would be appreciated.
EDIT - 1
class ConvertOrganizationToAwesomeNestedSet < ActiveRecord::Migration
def self.up
# add_column :party, :lft, :integer
# add_column :party, :rgt, :integer
Organization.reset_column_information
Organization.rebuild!
end
def self.down
remove_column :party, :lft
remove_column :party, :rgt
end
end
Model - Organization
class Organization < Party
attr_accessible :name,
:parent_id
default_scope :order => 'name'
acts_as_nested_set has_many :children, :class_name => 'Organization',
:foreign_key => "parent_id"
belongs_to :parent, :class_name =>'Organization', :foreign_key => "parent_id"
def display_name
name
end
You are using ActiveRecord model class in your migration. In short: don't.
This is likely to cause problems in the long run. If you have a migration that uses a model class, and than run it again later on, you'll be in trouble if anyone changes that model class.
Migrations are strictly for setting up the database, and should be constructed so that they will always behave in the same way - regardless of what the current state of your app's code is.
If you really need a migration to an ActiveRecord model: Define that class inside the migration file. This way it will be immutable to changes to the rest of the app.
Since you're getting
Validation failed: Name can't be blank
you must have a
validates_presence_of :name
somewhere, either on your class or in the plugin.
Maybe you have some invalid records in your database, and when you rebuild!, they are saved (and validated) and you get this error.