Bathakarai
Bathakarai

Reputation: 1537

Rails migration error - Name can't be blank

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.

  1. Get the application code from github.
  2. Run schema:create, load and seed process
  3. After that add some data in the application.
  4. Again Pull the code from the same branch.
  5. 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

Upvotes: 0

Views: 950

Answers (2)

sandro d.
sandro d.

Reputation: 156

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.

Upvotes: 0

averell
averell

Reputation: 3772

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.

Upvotes: 1

Related Questions