Posted by on 10/03/2011
Problem:

While I was updating my entry on the database, this error appeared. Together with a very long log file that goes like this:



[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/whiny_nil.rb:52:in `method_missing'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:808:in `validates_uniqueness_of'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:480:in `validates_each'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:477:in `each'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:477:in `validates_each'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:182:in `call'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:182:in `evaluate_method'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:166:in `call'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:90:in `run'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:90:in `each'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:90:in `send'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:90:in `run'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:276:in `run_callbacks'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:1110:in `valid_without_callbacks?'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/callbacks.rb:315:in `valid?'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:987:in `validates_associated'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:369:in `method_missing'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:215:in `collect'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:215:in `send'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:215:in `method_missing'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:369:in `method_missing'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:987:in `validates_associated'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:480:in `validates_each'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:477:in `each'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:477:in `validates_each'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:182:in `call'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:182:in `evaluate_method'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:166:in `call'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:90:in `run'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:90:in `each'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:90:in `send'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:90:in `run'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:276:in `run_callbacks'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:1110:in `valid_without_callbacks?'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/callbacks.rb:315:in `valid?'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:1089:in `save_without_dirty'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/dirty.rb:79:in `save_without_transactions'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:229:in `send'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:229:in `with_transaction_returning_status'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:182:in `transaction'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:228:in `with_transaction_returning_status'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:196:in `save'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/transactions.rb:196:in `save'
[INSTANT_RAILS_DIRECTORY]/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2664:in `update_attributes'
...



Analysis:

As you can see from the log above, there is something wrong going on with your validation in which, in my case, happens on validates unqiueness.

Checking my code, I find a validation that goes on like this:

belongs_to :course
  belongs_to :subject
  
  attr_accessor :should_destroy
  validates_uniqueness_of :subject, :scope => [:course_id]

From here, you can easily see the cause of the error.

Solution:

From the code above, changing subject to subject_id does the work. So it would be:

validates_uniqueness_of :subject_id, :scope => [:course_id]

This is because subject, which is a foreign key doesn't exist as a column for the said model. So adding _id solves the issue.

2 comments :

  1. ruby.. cool! I hope I could learn web dev soon. :)

    ReplyDelete
  2. Thanks! I had changed a column from a name:string to association_id:integer and forgot that I had validation on the naem:string that was still in the model. Your post helped a lot. :)

    ReplyDelete