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

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.

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

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

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,

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

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

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

I think this may be helpful

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

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

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.