Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

purpose of ActiveRecord::Base.reset_subclasses?

39 views
Skip to first unread message

Xavier Noria

unread,
Nov 19, 2007, 5:26:26 AM11/19/07
to rubyonra...@googlegroups.com
ActionController::Dispatcher#cleanup_application does this:

def cleanup_application(force = false)
if Dependencies.load? || force
ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord)
Dependencies.clear
ActiveRecord::Base.clear_reloadable_connections! if
defined?(ActiveRecord)
end
end

where

def self.reset_subclasses #:nodoc:
nonreloadables = []
subclasses.each do |klass|
unless Dependencies.autoloaded? klass
nonreloadables << klass
next
end
klass.instance_variables.each { |var|
klass.send(:remove_instance_variable, var) }
klass.instance_methods(false).each { |m| klass.send :undef_method, m }
end
@@subclasses = {}
nonreloadables.each { |klass| (@@subclasses[klass.superclass]
||= []) << klass }
end

Why do ARs need that cleanup?

-- fxn

Tarmo Tänav

unread,
Nov 19, 2007, 6:16:18 AM11/19/07
to rubyonra...@googlegroups.com

On E, 2007-11-19 at 11:26 +0100, Xavier Noria wrote:
> Why do ARs need that cleanup?

Probably because they have to reload the database schema
which is partly described in their class instance variables
on every request.

For example, columns:

# Returns an array of column objects for the table associated with
this class.
def columns
unless @columns
@columns = connection.columns(table_name, "#{name} Columns")
@columns.each {|column| column.primary = column.name ==
primary_key}
end
@columns
end

--
Tarmo Tänav <ta...@itech.ee>

f...@hashref.com

unread,
Nov 19, 2007, 6:53:21 AM11/19/07
to Ruby on Rails: Core
On Nov 19, 12:16 pm, Tarmo Tänav <ta...@itech.ee> wrote:

> On E, 2007-11-19 at 11:26 +0100, Xavier Noria wrote:
>
> > Why do ARs need that cleanup?
>
> Probably because they have to reload the database schema
> which is partly described in their class instance variables
> on every request.

But if the class was autoloaded wouldn't that be a side-effect of
remove_const Model + const_missing Model as with the rest of
autoloaded classes? Why are those special removals needed?

-- fxn

Michael Koziarski

unread,
Nov 20, 2007, 1:20:13 AM11/20/07
to rubyonra...@googlegroups.com
> But if the class was autoloaded wouldn't that be a side-effect of
> remove_const Model + const_missing Model as with the rest of
> autoloaded classes? Why are those special removals needed?

Indeed that does seem unneeded at first glance. Another thing to test
out once 2.0 is out the door I guess ;)

--
Cheers

Koz

Reply all
Reply to author
Forward
0 new messages