after initialize blowing up---help

25 views
Skip to first unread message

ddoherty03

unread,
Jun 11, 2011, 4:45:03 AM6/11/11
to rubyonra...@googlegroups.com
All,

Can anyone see what's happening here?

================== migration =======================
class CreateGreetings < ActiveRecord::Migration
  def self.up
    create_table :greetings do |t|
      t.string :greet
      t.string :language
      t.integer :count

      t.timestamps
    end
  end

  def self.down
    drop_table :greetings
  end
end
================== greeting.rb =============================
class Greeting < ActiveRecord::Base
  after_initialize :check_greet
  validates_uniqueness_of :count

  def check_greet
    self.language = lanugage.capitalize
  end
end
=================== greeting.yml ==========================
hello:
  greet: howdy
  language: english
  count: 44

hola:
  greet: hola, que tal
  language: spanish
  count: 55

=================== greeting_test.rb ========================
require 'test_helper'

class GreetingTest < ActiveSupport::TestCase
  # Replace this with your real tests.
  test "uniqueness of count" do
    gg = Greeting.new(:language => 'esperanto', :count => greetings(:hello).count)
    assert !gg.valid?
  end
end
=========================================================

When I run 'rake test:units' on this it blows up when getting to the after_initialize
procedure, claiming there is no method 'language' for the Greeting class.
I'm having this issue in a larger app, but I've tried to pare this down to the minimum
that reproduces the problem.

Is this a bug in rails, or am I missing something?

Regards,

Colin Law

unread,
Jun 11, 2011, 4:54:42 AM6/11/11
to rubyonra...@googlegroups.com

Have you run the migration?

What does db/schema.rb show for the greetings table?

Colin

ddoherty03

unread,
Jun 11, 2011, 5:29:28 AM6/11/11
to rubyonra...@googlegroups.com
Colin,

Yes.  I have run the migration.  Here is the schema:

======================== schema.rb ===================
ActiveRecord::Schema.define(:version => 20110609145209) do

  create_table "greetings", :force => true do |t|
    t.string   "greet"
    t.string   "language"
    t.integer  "count"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end
====================================================

ddoherty03

unread,
Jun 11, 2011, 5:37:39 AM6/11/11
to rubyonra...@googlegroups.com
Oh, and this might be relevant:

$ rails -v
Rails 3.0.3

$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]

Thanks.

Colin Law

unread,
Jun 11, 2011, 5:45:33 AM6/11/11
to rubyonra...@googlegroups.com
On 11 June 2011 09:45, ddoherty03 <ddohe...@gmail.com> wrote:

This shows how important it is to always copy/paste error messages and
code. I think if you look carefully at the error message you may see
that it says that there is no method lanugage rather than language.
Check the code in check_greet

Colin

Michael Pavling

unread,
Jun 11, 2011, 6:21:17 AM6/11/11
to rubyonra...@googlegroups.com

Sorry for the top post, but I thought it would be worth raising the point that overloading after_initialize on AR::Base was a bad idea (or at least used to be).

Have a quick Google of that to see what comes up.
 

Top-posted from Android

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/hxqM0KZoDbUJ.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

ddoherty03

unread,
Jun 11, 2011, 6:40:30 AM6/11/11
to rubyonra...@googlegroups.com
Colin,

Good catch.  When I correct the typo, though, I still get the same error.
I get this in another app too, so this seems to be something else going on here.

Regards,

Colin Law

unread,
Jun 11, 2011, 6:45:24 AM6/11/11
to rubyonra...@googlegroups.com

You should not have to restart the server, but try that (if you have
not already).
Please post the code of the method where the failure is occurring
(copy and paste) and the full error message (also copy and paste).

Colin

ddoherty03

unread,
Jun 11, 2011, 9:25:15 AM6/11/11
to rubyonra...@googlegroups.com
Here is the method that is blowing up:
===================== greeting.rb ==============================
class Greeting < ActiveRecord::Base
  after_initialize :check_greet
  validates_uniqueness_of :count

  def check_greet
    self.language = language.capitalize
  end
end
============================================================
And here is the backtrace I get when I run the unit test posted earlier:
============================================================
Loaded suite /home/ded/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.1/lib/rake/rake_test_loader
Started
E
Finished in 0.280612 seconds.

  1) Error:
test_uniqueness_of_count(GreetingTest):
ActiveModel::MissingAttributeError: missing attribute: language
    /home/ded/Rails/demo/app/models/greeting.rb:6:in `check_greet'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:415:in `_run_initialize_callbacks'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activerecord-3.0.3/lib/active_record/base.rb:1453:in `init_with'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activerecord-3.0.3/lib/active_record/base.rb:909:in `instantiate'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activerecord-3.0.3/lib/active_record/base.rb:467:in `block in find_by_sql'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activerecord-3.0.3/lib/active_record/base.rb:467:in `collect!'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activerecord-3.0.3/lib/active_record/base.rb:467:in `find_by_sql'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activerecord-3.0.3/lib/active_record/relation.rb:64:in `to_a'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activerecord-3.0.3/lib/active_record/relation/finder_methods.rb:333:in `find_first'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activerecord-3.0.3/lib/active_record/relation/finder_methods.rb:122:in `first'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activerecord-3.0.3/lib/active_record/relation/finder_methods.rb:180:in `exists?'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activerecord-3.0.3/lib/active_record/validations/uniqueness.rb:39:in `validate_each'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activemodel-3.0.3/lib/active_model/validator.rb:154:in `block in validate'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activemodel-3.0.3/lib/active_model/validator.rb:151:in `each'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activemodel-3.0.3/lib/active_model/validator.rb:151:in `validate'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:314:in `_callback_before_7'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:414:in `_run_validate_callbacks'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activemodel-3.0.3/lib/active_model/validations.rb:212:in `run_validations!'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activemodel-3.0.3/lib/active_model/validations/callbacks.rb:67:in `block in run_validations!'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:413:in `_run_validation_callbacks'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activemodel-3.0.3/lib/active_model/validations/callbacks.rb:67:in `run_validations!'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activemodel-3.0.3/lib/active_model/validations.rb:179:in `valid?'
    /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activerecord-3.0.3/lib/active_record/validations.rb:55:in `valid?'
    /home/ded/Rails/demo/test/unit/greeting_test.rb:7:in `block in <class:GreetingTest>'

1 tests, 0 assertions, 0 failures, 1 errors, 0 skips

Test run options: --seed 44283
rake aborted!
Command failed with status (1): [/home/ded/.rvm/rubies/ruby-1.9.2-p180/bin/...]

ddoherty03

unread,
Jun 11, 2011, 9:37:01 AM6/11/11
to rubyonra...@googlegroups.com
Pavling,

Thanks for the comment.  If I understand it, though, I don't believe that's what's going on here.

I am not redefining the after_initialize method, just using it to add a callback into the object
creation stream.  This is pretty standard.  But when the validation test is run, it seems the attribute
methods suddenly go missing.

I spent several hours trying to trace this in the debugger, but I found myself in a deeply-nested call stack
of AR code and could not tell what was going on.

I decided to create a whole new minimal Rails app to see if I could reproduce it.  That's what this silly
greeting app is all about.  I've posted all the code in the entire app: just a migration, a simple model class, a fixture,
and a unit test.  That's why I think it might be a Rails bug, but I wanted to run it by the gurus here.

Hope this this helps.

Regards,

Dan

Colin Law

unread,
Jun 11, 2011, 10:28:12 AM6/11/11
to rubyonra...@googlegroups.com
> ....

>
> /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activemodel-3.0.3/lib/active_model/validations.rb:179:in
> `valid?'
>
> /home/ded/.rvm/gems/ruby-1.9.2-p180@rails303/gems/activerecord-3.0.3/lib/active_record/validations.rb:55:in
> `valid?'
>     /home/ded/Rails/demo/test/unit/greeting_test.rb:7:in `block in
> <class:GreetingTest>'
> 1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
> Test run options: --seed 44283
> rake aborted!
> Command failed with status (1):
> [/home/ded/.rvm/rubies/ruby-1.9.2-p180/bin/...]

I think this may be helpful
http://stackoverflow.com/questions/1778374/validates-presence-of-causes-after-initialize-to-be-called-with-a-weird-self

Colin

ddoherty03

unread,
Jun 11, 2011, 11:36:44 AM6/11/11
to rubyonra...@googlegroups.com
Colin,

Many thanks again.  That looks like it's the problem.  Also looks like it's been around about 3 years.

Regards,

Dan

Michael Pavling

unread,
Jun 11, 2011, 4:08:01 PM6/11/11
to rubyonra...@googlegroups.com

Sorry.
Yes, it's just plain old 'initialize' that shouldn't be overloaded, which you're not doing ...
Ignore me :-)

Top-posted from Android

-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk...

To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/cD7QR0b2juYJ.

To post to this group, send email to rubyonra...@googlegroups.com. To unsubscribe from this gr...

Frederick Cheung

unread,
Jun 11, 2011, 5:17:39 PM6/11/11
to Ruby on Rails: Talk


On Jun 11, 4:36 pm, ddoherty03 <ddohert...@gmail.com> wrote:
> Colin,
>
> Many thanks again.  That looks like it's the problem.  Also looks like it's
> been around about 3 years.
>

I think this was fixed quite recently in one of the 3.0.x versions
(not sure which but it would have been in 3.0.6 or later

Fred
> Regards,
>
> Dan

ddoherty03

unread,
Jun 12, 2011, 7:12:05 AM6/12/11
to rubyonra...@googlegroups.com
All,

In case you are following this, Frank is right, this was fixed in rails 3.0.7 in April, 2011.  I 
upgraded to rails 3.0.8. and the problem went away.

Regards,

Dan.

ddoherty03

unread,
Jun 12, 2011, 7:12:41 AM6/12/11
to rubyonra...@googlegroups.com
Oops, I meant Frederick.

Reply all
Reply to author
Forward
0 new messages