Hi,
I''m new to rails and I''m just playing around a little bit with
it. I
stumpled upon the following problem in development mode which seems to
be related to rails automatic dependancy-loading:
Suppose, you have a model "user.rb" and a
"user_controller.rb" with no
content. Now, I''m adding the following to User:
class User < ActiveRecord::Base
logger.info("Loading class User")
end
When calling ".../user/new", I get the following in my logs:
---
Loading class User
Loading class User
Processing UserController#new (for 127.0.0.1 at Thu Feb 10 01:22:23 CET
2005)
Parameters: {"action"=>"new",
"controller"=>"user"}
---
So, User is loaded twice. The reason that this is bad is, that if User
has a "has_and_belongs_to_many :foo", the before_save and after_save
callbacks get added twice and this causes ActiveRecord to try adding the
same record twice to the intermediate join table --- resulting in a
duplicate primary key exception from the database. I think this might be
related to issue #563 and #554.
Is this the expected behaviour or is it a bug?
A possible fix seems to be to set "Dependencies.mechanism = :require"
in
environments/development.rb, as this prevents loading the file twice.
I tried debugging the thing a little bit more, but do not understand
enough of rails dependancy handling yet. The problem seems to be in
action_controller/dependencies.rb in the "inherited" method. There is
a line
Object.const_get(child.controller_name.singularize.classify)
which will result in Object.const_get("user") when loading
UserController which will result in const_missing which in turn will
load "user.rb" directly without using "depend_on" so that a
subsequent
"depend_on" will load "user.rb" again. Did this make any
sense at all?
Any thoughts or advice on this?
Sebastian
> which will result in Object.const_get("user") when loading > UserController which will result in const_missing which in turn will > load "user.rb" directly without using "depend_on" so that a subsequent > "depend_on" will load "user.rb" again. Did this make any sense at all?In newer Rails, you don''t ever need to explicitly require or depend on any model class. It''s all handled for you. As long as you follow User => user.rb. So remove all your explicit requires and life will be good again. -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://macromates.com/ -- TextMate: Code and markup editor (OS X) http://www.loudthinking.com/ -- Broadcasting Brain
Hi David, David Heinemeier Hansson wrote:> In newer Rails, you don''t ever need to explicitly require or depend on > any model class. It''s all handled for you. As long as you follow User > => user.rb. So remove all your explicit requires and life will be good > again.thanks for the quick reply, but this does not solve the problem, because I''m not using any requires or loads. I''ll try to express my problem more concise (and easily reproducable) this time: I''m creating a new rails application and just have one controller and one model: skanthak@mercur rails-test $ cat app/controllers/user_controller.rb class UserController < ApplicationController end skanthak@mercur rails-test $ cat app/models/user.rb class User < ActiveRecord::Base logger.info("Loading class User") end When I now start "./script/server" in development mode and access ".../user/new" I get the following in my logs (besides an exception for the missing action new, of course): --- Loading class User Loading class User Processing UserController#new (for 127.0.0.1 at Thu Feb 10 10:48:53 CET 2005) Parameters: {"action"=>"new", "controller"=>"user"} ActionController::UnknownAction (No action responded to new): --- Notice that User is loaded twice. While this does not cause a problem in this example it does as soon as User has a "has_and_belongs_to_many" attribute, because before_save and after_save callbacks are added twice and this leads to SQL-statements being executed twice resulting in a duplicate primary key error. Did this clarify my problem? Am I doing something wrong? My classes are as simple as possible... Sebastian
hi have the same problems...i posted a ticket...#563...doesn''t know if it will be corrected in 0.9.6.... On 10 févr. 05, at 10:56, Sebastian Kanthak wrote:> Hi David, > > David Heinemeier Hansson wrote: > >> In newer Rails, you don''t ever need to explicitly require or depend >> on any model class. It''s all handled for you. As long as you follow >> User => user.rb. So remove all your explicit requires and life will >> be good again. > > thanks for the quick reply, but this does not solve the problem, > because I''m not using any requires or loads. I''ll try to express my > problem more concise (and easily reproducable) this time: > > I''m creating a new rails application and just have one controller and > one model: > > skanthak@mercur rails-test $ cat app/controllers/user_controller.rb > class UserController < ApplicationController > end > skanthak@mercur rails-test $ cat app/models/user.rb > class User < ActiveRecord::Base > logger.info("Loading class User") > end > > When I now start "./script/server" in development mode and access > ".../user/new" I get the following in my logs (besides an exception > for the missing action new, of course): > > --- > Loading class User > Loading class User > > > Processing UserController#new (for 127.0.0.1 at Thu Feb 10 10:48:53 > CET 2005) > Parameters: {"action"=>"new", "controller"=>"user"} > > > ActionController::UnknownAction (No action responded to new): > --- > > Notice that User is loaded twice. While this does not cause a problem > in this example it does as soon as User has a > "has_and_belongs_to_many" attribute, because before_save and > after_save callbacks are added twice and this leads to SQL-statements > being executed twice resulting in a duplicate primary key error. > > Did this clarify my problem? Am I doing something wrong? My classes > are as simple as possible... > > Sebastian > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >