ActiveRecord inheritance

How to create a super class based on ActiveRecord::Base?

For example:

class ParentRecord < ActiveRecord::Base
end

class ChildRecord < ParentRecord
end

Rails always asks for ‘parent_records’ table. Which has absolutely no
need to exist at all. It’s intentionally made as a super class to be
inherited.

Thanks.

On 7/13/06, iseng [email protected] wrote:

Rails always asks for ‘parent_records’ table. Which has absolutely no
need to exist at all. It’s intentionally made as a super class to be
inherited.

Thanks.

By doing this rails assumes you want to use single table inheritance.
To do
this you have a parent_records table with a type (string) field.

There is a fair bit of information regarding STI kicking around
especially
on the wiki

Daniel ----- wrote:

By doing this rails assumes you want to use single table inheritance.
To do
this you have a parent_records table with a type (string) field.

There is a fair bit of information regarding STI kicking around
especially
on the wiki

Hi Daniel thanks for the reply.

The thing is, I don’t want to use Single Table Inheritance. I have
several record objects based on TextRecord (which incorporates all text
formatting functions).

ActiveRecord::Base > TextRecord
TextRecord > ForumPost
TextRecord > Article
TextRecord > PrivateMessage
etc

I want separate tables for each and everyone: forum_posts, articles,
private_messages, etc.

On 7/13/06, iseng [email protected] wrote:


TextRecord > PrivateMessage
etc

I want separate tables for each and everyone: forum_posts, articles,
private_messages, etc.

In this case, you would be best to put the common methods into a module
and
then include this module in the classes

eg
module MyModule
def my_method
do_something
end
end

Put this into the lib directory of your app structure and put the
include
statement into your classes
class PrivateMessage < ActiveRecord::Base
include MyModule

end
etc

This will provide the methods as instance methods on your classes.

On 7/13/06, iseng [email protected] wrote:

etc
class TextRecord < ActiveRecord::Base
self.abstract_class = true
end

class ForumPost < TextRecord
end

etc…

Tom

On 7/13/06, Daniel N [email protected] wrote:

There is a fair bit of information regarding STI kicking around

In this case, you would be best to put the common methods into a module
statement into your classes
class PrivateMessage < ActiveRecord::Base
include MyModule

end
etc

This will provide the methods as instance methods on your classes.

Or do what Tom suggests :wink: Thanx Tom. I missed that one

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs