Subclassing ActiveRecord::Base

First of all, sorry for the crossposting, but I put this into the Ruby
Forum first of all, but was pointed to this as a more appropriate
location.

I’m using ActiveRecord and ActiveSupport in a non-rails environment
to connect to multiple databases, and I’ve found the following (single
database) to cause me an error. Note that params is my database settings
and omitted for obvious reasons. pages, components, and books are all
tables in the same db.

require ‘active_record’
module TestDB
class Inherited < ActiveRecord::Base
self.logger
self.default_timezone :utc
self.establish_connection(params)
end
class Page < Inherited
puts self.name
puts self.table_name
end
class Book < ActiveRecord::Base
puts self.name
puts self.table_name
end
end
class Component < ActiveRecord::Base
puts self.name
puts self.table_name
end

outputs:

TestDB::Page <= expected
inheriteds <= Hmm, table name for the parent?
TestDB::Book <= also expected
books <= Seems to be using the Inherited connection
Component <= Yep, no module name
components <= also using the Inherited connection

Is this expected behaviour from an inherited class. Is there anyway to
scope the ActiveRecord connection without resorting to plugins or other
gems?

Mac

On Jan 26, 5:51 pm, Paul M. [email protected] wrote:

require ‘active_record’
module TestDB
class Inherited < ActiveRecord::Base
self.logger
self.default_timezone :utc
self.establish_connection(params)

You should set self.abstract_class = true on this class or
activerecord might think you’re trying single table inheritance.

Fred

Frederick C. wrote:

On Jan 26, 5:51�pm, Paul M. [email protected] wrote:

require ‘active_record’
module TestDB
� class Inherited < ActiveRecord::Base
� � self.logger
� � self.default_timezone :utc
� � self.establish_connection(params)

You should set self.abstract_class = true on this class or
activerecord might think you’re trying single table inheritance.

Fred

I’ve used inheritance with many of my models. To expand upon what Fred
is saying here, you really need to do two things. I’ll show you an
example use and then you can configure your own class to use it:

One of my models is an inheritance template:

class InheritanceTemplate < ActiveRecord::Base
self.abstract_class = true
end

Other models inherit from this template by doing the following:

class MyPage < InheritanceTemplate
set_table_name “my_pages” #pluralized table name
end

etc.

Notice in the classes that are using the inheritance template you should
also set the table name so that there’s no confusion going on in the
templates.

On Jan 26, 8:12 pm, Alpha B. [email protected] wrote:

Notice in the classes that are using the inheritance template you should
also set the table name so that there’s no confusion going on in the
templates.

I’ve never needed to do that.

Fred

Alpha B. wrote:

Frederick C. wrote:

On Jan 26, 5:51�pm, Paul M. [email protected] wrote:
class MyPage < InheritanceTemplate
set_table_name “my_pages” #pluralized table name
end

etc.

Notice in the classes that are using the inheritance template you should
also set the table name so that there’s no confusion going on in the
templates.

I was doing that in the models that I had inherited, but if there isn’t
a way to do it without setting the table name then I don’t see the
benefit.

Paul

Frederick C. wrote:

On Jan 26, 8:12�pm, Alpha B. [email protected] wrote:

Notice in the classes that are using the inheritance template you should
also set the table name so that there’s no confusion going on in the
templates.

I’ve never needed to do that.

Fred

It depends on what he’s going to do with the code. In my case, I have
several archive tables that apply to some of my inheritance models. I
only placed it in there as an example. :slight_smile:

Paul M. wrote:

Alpha B. wrote:

Frederick C. wrote:

On Jan 26, 5:51�pm, Paul M. [email protected] wrote:
class MyPage < InheritanceTemplate
set_table_name “my_pages” #pluralized table name
end

etc.

Notice in the classes that are using the inheritance template you should
also set the table name so that there’s no confusion going on in the
templates.

I was doing that in the models that I had inherited, but if there isn’t
a way to do it without setting the table name then I don’t see the
benefit.

As Fred said, setting the table name is probably unnecessary.

Paul

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]