Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #2819 (closed defect: fixed)

Opened 4 years ago

Last modified 3 years ago

model reloading fails when nested in module

Reported by: orlando.doehring@hpi.uni-potsdam.de Assigned to: csshsh
Priority: high Milestone: 1.1
Component: ActiveRecord Version: 1.0.0
Severity: major Keywords: undefined method error needs_review
Cc:

Description (Last modified by ulysses)

I have a bug concerning ActiveRecord. It cannot find a method in my ActiveRecord ORM mapping. I have the following. Given two mysql-database tables in one-to-many association:

1.) Country

class Db::Country < ActiveRecord::Base
  has_many :regions
end

2.) Region

class Db::Region < ActiveRecord::Base
  has_many  :cities
  belongs_to :country
end

I have a class ../app/managers/location_manager.rb and one of the method is:

def get_cities_by_country( country, language )
    # check whether countries and regions firstly need to be retrieved from web service
    get_countries_and_regions [[BR]]
    coun = Country.find(:first, :conditions => "isoCode='#{country}'") [[BR]]
    puts coun.methods.sort // line 270 [[BR]]
    reg = coun.regions // line 271 [[BR]]
    ...
end 

This method shall get called by search method in the class ../app/controllers/inform_controller.rb:

def index [[BR]]
    search [[BR]]end

def search
    ...  [[BR]]
    @locMan = LocationManager.new [[BR]]
    # get all French countries in German [[BR]]
    @cities = @locMan.get_cities_by_country('FR', 'DE') [[BR]]
    ... [[BR]]
end        

The problem is as follows: My web page invokes the "index" method of the "inform_controller", this goes to the "search" method, and this goes to the "get_cities_by_country" method of the "location_manager". When I first load my web page no error occurrs and every successive loading of my web page results in the following error:

undefined method `regions' for #<Db::Country:0x10945610>
RAILS_ROOT: script/../config/..

Application Trace | Framework Trace | Full Trace [[BR]]
./script/../config/../vendor/rails/activerecord/lib/active_record/base.rb:1497:in `method_missing'[[BR]]
./script/../config/../app/managers/location_manager.rb:271:in `get_cities_by_country' [[BR]]
./script/../config/../app/controllers/inform_controller.rb:24:in `search' [[BR]]
./script/../config/../app/controllers/inform_controller.rb:4:in `index' [[BR]]

And it seems it is a bug in the ActiveRecord and as shown by line 270, indeed it does not list the method regions, regions= at the second, third, ... time of loading my web page.

Ruby version: ruby 1.8.3 (2005-09-21) [i386-cygwin] Rails version: Rails 0.14.1 Updated by svn trunk in repository.

Help would be highly appreciated! I also will add further information about the error if requested as the account of discussion may not be detailed enough.

Attachments

error_example_rails.tar.gz (74.7 kB) - added by okax@gmx.de on 11/21/05 16:09:18.
example rails environment to reproduce the error

Change History

11/10/05 16:39:19 changed by bitsweat

  • priority changed from normal to high.
  • severity changed from normal to major.

11/16/05 23:46:15 changed by Florian@oaklandcutlery.com

Orlando, could you attach the code and db scheme for this app? If you can not or don't want to send the whole code, your models and a small example code that causes the 'undefined method'-exception would be also good enough. Thanks!

11/16/05 23:51:43 changed by ulysses

  • description changed.

11/17/05 11:17:32 changed by orlando.doehring@hpi.uni-potsdam.de

Hi Florian,

I try to create a minimal example, e.g. a Rails project, where I encounter the error. And I would like to add that the test I had written for that method passes:

require 'test/test_helper' class LocationManager

... def test_get_cities_by_country

# try to retrieve cities from Spain in German cities = @locMan.get_cities_by_country('FR', 'DE') # make sure that we get Spain's cities assert_not_nil(cities, "location manager should return cities") if cities.nil? then puts cities[0].inspect end

end ...

end

11/17/05 13:44:41 changed by andreas.buerk@octaid.de

have the same problem (instable behaviour). Everything worked fine - after restart WEBrick (added a new database table), all pages of this controller are broken. Recovered the database to the former model and restarted webrick again without any effect. The error is still the same.

ruby 1.8.2 (2004-12-25) / rails 0.14.3 / mysql 5.0.15

11/21/05 16:02:18 changed by okax@gmx.de

Hello,

I worked with Orlando and we try to isolate the error. It occurs only, if the model is within a module and only in the 'development' environment, not in 'productive' one.

I will attach an archive with a sample rails tree. Please configure the database.yml and load the db/development_struct.sql. You have to start the server and display the site in your browser. The first time the site will be displayed. Then try to refresh the site and the error will occur.

Olaf

11/21/05 16:09:18 changed by okax@gmx.de

  • attachment error_example_rails.tar.gz added.

example rails environment to reproduce the error

11/21/05 16:10:50 changed by okax@gmx.de

You also have to create an entry to the countries table with the isoCode 'FR'.

Olaf

11/21/05 19:42:46 changed by bitsweat

  • keywords changed from ActiveRecord 'undefined method' error to undefined method error.
  • owner changed from David to Ulysses.
  • summary changed from ActiveRecord 'undefined method' error - cannot find method defined by one-to-many association to model reloading fails when nested in module.
  • milestone set to 1.0.

11/24/05 17:33:22 changed by Florian Weber <csshsh@gmail.com>

  • owner changed from Ulysses to csshsh.

11/29/05 14:17:55 changed by sixtus@gmail.com

I have the same bug, but the trigger is different. I am calling instance_eval within my active record classes. Switching to production fixes it as well.

01/03/06 08:39:08 changed by moisesdeniz@hotmail.com

I have the same bug. I have a module named 'Personal' and my schema is too simple:

module Personal

class Empleado < ActiveRecord::Base

belongs_to :destino

end

class Destino < ActiveRecord::Base

has_many :empleados, :class_name => "Personal::Empleado"

end

end

When I start Webrick there is no problem, but the second time I get the 'method missing error':

undefined method `destino' for #<Personal::Empleado:0xb78b2c98>

/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1501:in `method_missing' #{RAILS_ROOT}/app/views/layouts/application.rhtml:20 /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/base.rb:268:in `send' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/base.rb:268:in `compile_and_render_template' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/base.rb:244:in `render_template' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/base.rb:205:in `render_file' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/layout.rb:226:in `render_without_benchmark' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:53:in `render' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:53:in `measure' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:53:in `render' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:854:in `perform_action_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/filters.rb:332:in `perform_action_without_benchmark' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in `measure' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/rescue.rb:82:in `perform_action' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:in `send' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:in `process_without_session_management_support' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/session_management.rb:116:in `process' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in `dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in `handle_dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:in `service' /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' /usr/lib/ruby/1.8/webrick/server.rb:172:in `start_thread' /usr/lib/ruby/1.8/webrick/server.rb:161:in `start' /usr/lib/ruby/1.8/webrick/server.rb:161:in `start_thread' /usr/lib/ruby/1.8/webrick/server.rb:95:in `start'

/usr/lib/ruby/1.8/webrick/server.rb:92:in `each' /usr/lib/ruby/1.8/webrick/server.rb:92:in `start' /usr/lib/ruby/1.8/webrick/server.rb:82:in `start' /usr/lib/ruby/1.8/webrick/server.rb:82:in `start' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in `dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59 /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require' /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require' /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in `require' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28 /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require' /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require' /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in `require' script/server:3

01/20/06 17:27:14 changed by robbyrussell

  • version changed from 0.14.1 to 1.0.0.
  • milestone changed from 1.0 to 1.1.

Okay, this is really odd. I encountered the same issue yesterday.

irb(#<ContactTypesController:0x262e570>):009:0> ContactType
=> ContactType
irb(#<ContactTypesController:0x262e570>):010:0> ContactType.methods.include? 'count'
=> true
irb(#<ContactTypesController:0x262e570>):011:0> ContactType.superclass
=> ActiveRecord::Base
irb(#<ContactTypesController:0x262e570>):012:0> 
Server exited. Closing connection...

Reload page...

No connection to breakpoint service at druby://localhost:42531 (DRb::DRbConnError)
Tries to connect will be made every 2 seconds...
Executing break point at ./script/../config/../app/controllers/contact_types_controller.rb:6 in `index'
irb(#<ContactTypesController:0x277b93c>):001:0> ContactType
=> ContactType
irb(#<ContactTypesController:0x277b93c>):002:0> ContactType.superclass
=> Object
irb(#<ContactTypesController:0x277b93c>):003:0> ContactType.methods.include? 'count'
=> false

This does not happen in production mode... just development.

03/07/06 11:46:40 changed by Tomas Jogin

I have the same or similar bug, in my case I get Uninitialized Constant on every but the _first_ request, both in development mode and production mode.

Everything worked fine in Revision 3636, but since Revision 3637 it does not anymore.

03/26/06 02:07:55 changed by anonymous

  • keywords changed from undefined method error to undefined method error needs_review.

Please consider including a fix for this in 1.1 release.

03/26/06 22:46:18 changed by Florian Weber <csshsh@gmail.com>

  • status changed from new to closed.
  • resolution set to fixed.

Fixed with [4049]