After initialize blowing up---help

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,

On 11 June 2011 09:45, ddoherty03 [email protected] wrote:

end
self.language = lanugage.capitalize
count: 55

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.

Have you run the migration?

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

Colin

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

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.

On 11 June 2011 09:45, ddoherty03 [email protected] wrote:

end
self.language = lanugage.capitalize
count: 55

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?

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

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,

On 11 June 2011 11:40, ddoherty03 [email protected] wrote:

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.

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

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

On Jun 11, 2011 9:45 AM, “ddoherty03” [email protected] wrote:

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,


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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

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/[email protected]/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/[email protected]/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:415:in
`_run_initialize_callbacks’

/home/ded/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/base.rb:1453:in
`init_with’

/home/ded/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/base.rb:909:in
`instantiate’

/home/ded/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/base.rb:467:in
`block in find_by_sql’

/home/ded/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/base.rb:467:in
`collect!’

/home/ded/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/base.rb:467:in
`find_by_sql’

/home/ded/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/relation.rb:64:in
`to_a’

/home/ded/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/relation/finder_methods.rb:333:in
`find_first’

/home/ded/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/relation/finder_methods.rb:122:in
`first’

/home/ded/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/relation/finder_methods.rb:180:in
`exists?’

/home/ded/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/validations/uniqueness.rb:39:in
`validate_each’

/home/ded/.rvm/gems/[email protected]/gems/activemodel-3.0.3/lib/active_model/validator.rb:154:in
`block in validate’

/home/ded/.rvm/gems/[email protected]/gems/activemodel-3.0.3/lib/active_model/validator.rb:151:in
`each’

/home/ded/.rvm/gems/[email protected]/gems/activemodel-3.0.3/lib/active_model/validator.rb:151:in
`validate’

/home/ded/.rvm/gems/[email protected]/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:314:in
`_callback_before_7’

/home/ded/.rvm/gems/[email protected]/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:414:in
`_run_validate_callbacks’

/home/ded/.rvm/gems/[email protected]/gems/activemodel-3.0.3/lib/active_model/validations.rb:212:in
`run_validations!’

/home/ded/.rvm/gems/[email protected]/gems/activemodel-3.0.3/lib/active_model/validations/callbacks.rb:67:in
`block in run_validations!’

/home/ded/.rvm/gems/[email protected]/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:413:in
`_run_validation_callbacks’

/home/ded/.rvm/gems/[email protected]/gems/activemodel-3.0.3/lib/active_model/validations/callbacks.rb:67:in
`run_validations!’

/home/ded/.rvm/gems/[email protected]/gems/activemodel-3.0.3/lib/active_model/validations.rb:179:in
`valid?’

/home/ded/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/validations.rb:55:in
valid?' /home/ded/Rails/demo/test/unit/greeting_test.rb:7:inblock 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/…]

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

On 11 June 2011 14:25, ddoherty03 [email protected] wrote:

And here is the backtrace I get when I run the unit test posted earlier:

/home/ded/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/validations.rb:55:in

valid?' /home/ded/Rails/demo/test/unit/greeting_test.rb:7:inblock 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

Colin,

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

Regards,

Dan

Sorry.
Yes, it’s just plain old ‘initialize’ that shouldn’t be overloaded,
which
you’re not doing …
Ignore me :slight_smile:

Top-posted from Android

On Jun 11, 2011 2:37 PM, “ddoherty03” [email protected] wrote:

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

– 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 [email protected]
To
unsubscribe from this gr…

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.

Oops, I meant Frederick.

On Jun 11, 4:36pm, ddoherty03 [email protected] 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