# Rails base model

**URL:** <https://www.ruby-forum.com/t/rails-base-model/195122>\
**Category:** Rails\
**Created:** [September 17, 2010, 9:53am UTC](https://www.ruby-forum.com/t/rails-base-model/195122 "2010-09-17T09:53:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielMedia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@DanielMedia](https://www.ruby-forum.com/u/DanielMedia)\
**Post date:** [September 17, 2010, 9:53am UTC](https://www.ruby-forum.com/t/rails-base-model/195122/1 "2010-09-17T09:53:29Z")

</div>

I’m very new to Ruby and to Rails. I’m coming from PHP and CakePHP.

I would like to create a base model from which all other models will  
inherit from.

I have done this:

class AppModel \< ActiveRecord::Base  
end

and

class Post \< AppModel  
end

I’m getting the following error:

Table ‘blog\_development.app\_models’ doesn’t exist

My question is, how do I tell Rails not to look for a table named  
“app\_models” AND am I even taking the right approach here? Is there a  
better way to do this?

---

<div class="post-metadata">

**Author:** ![DanielMedia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@DanielMedia](https://www.ruby-forum.com/u/DanielMedia)\
**Post date:** [September 17, 2010, 11:01am UTC](https://www.ruby-forum.com/t/rails-base-model/195122/2 "2010-09-17T11:01:16Z")

</div>

Hi Daniel,

Go through this link

> <https://stackoverflow.com/questions/937429/activerecordbase-without-table>

Where in, answers section you can find an example related to your  
approach.

–  
Regards,

T.Veerasundaravel.  
[http://tinyurl.com/25vma7h](http://tinyurl.com/25vma7h)  
@veerasundaravel

DanielMedia wrote:

> I’m very new to Ruby and to Rails. I’m coming from PHP and CakePHP.
> 
> I would like to create a base model from which all other models will  
> inherit from.
> 
> I have done this:
> 
> class AppModel \< ActiveRecord::Base  
> end
> 
> and
> 
> class Post \< AppModel  
> end
> 
> I’m getting the following error:
> 
> Table ‘blog\_development.app\_models’ doesn’t exist
> 
> My question is, how do I tell Rails not to look for a table named  
> “app\_models” AND am I even taking the right approach here? Is there a  
> better way to do this?

---

<div class="post-metadata">

**Author:** ![DanielMedia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@DanielMedia](https://www.ruby-forum.com/u/DanielMedia)\
**Post date:** [September 17, 2010, 12:08pm UTC](https://www.ruby-forum.com/t/rails-base-model/195122/3 "2010-09-17T12:08:08Z")

</div>

Hi there,

On Thu, 2010-09-16 at 18:12 -0700, DanielMedia wrote:

> and  
> better way to do this?  
> Short answer: Use set\_table\_name  
> [http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-set\_table\_name](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-set_table_name)  
> in Post. [Though I haven’t tried this and there may be further  
> complications.]

Long answer: You’re almost certainly doing the wrong thing. If you want  
to share code between non-similar models, don’t do it using inheritance.  
Create a module which encapsulates the functionality (people tend to put  
such stuff in the lib/ directory), and include the module in the models  
instead.

If it might be useful for several different projects, you could put your  
module in a plugin, and then have the module automatically include  
itself in ActiveRecord::Base (therefore making its behaviour available  
to all models).

Hope that helps,

Jon

–  
[http://jonathanleighton.com/](http://jonathanleighton.com/)

---

<div class="post-metadata">

**Author:** ![DanielMedia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@DanielMedia](https://www.ruby-forum.com/u/DanielMedia)\
**Post date:** [September 17, 2010, 12:22pm UTC](https://www.ruby-forum.com/t/rails-base-model/195122/4 "2010-09-17T12:22:47Z")

</div>

sorry to say, but your short answer is wrong.

> > class AppModel \< ActiveRecord::Base  
> > end

> > Table ‘blog\_development.app\_models’ doesn’t exist

> > My question is, how do I tell Rails not to look for a table named  
> > “app\_models” AND am I even taking the right approach here? Is there a  
> > better way to do this?

> Short answer: Use set\_table\_name  
> [http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-se...](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-se...) in Post.

the right (short) answer is:

class AppModel \< ActiveRecord::Base

# define as abstract.

self.abstract\_class = true  
end

> > I would like to create a base model from which all other models will inherit from.  
> > with that approach this is exactly what your getting. however, you  
> > don’t tell much about your goals. so it’s hard to tell, whether this  
> > is the right thing to do.

---

<div class="post-metadata">

**Author:** ![DanielMedia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@DanielMedia](https://www.ruby-forum.com/u/DanielMedia)\
**Post date:** [September 17, 2010, 9:01pm UTC](https://www.ruby-forum.com/t/rails-base-model/195122/5 "2010-09-17T21:01:29Z")

</div>

Thanks for the help everyone. I really just wanted to have a base  
model and have the option of overriding methods for all models or  
adding functionality etc. I will try Fred and MaD’s approach and see  
how that goes. I also realized that my initial idea to define a  
to\_slug method in the AppModel is probably wrong and should probably  
go within a module instead.

---

<div class="post-metadata">

**Author:** ![DanielMedia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@DanielMedia](https://www.ruby-forum.com/u/DanielMedia)\
**Post date:** [September 17, 2010, 12:18pm UTC](https://www.ruby-forum.com/t/rails-base-model/195122/6 "2010-09-17T12:18:05Z")

</div>

On Sep 17, 2:12 am, DanielMedia [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m getting the following error:
> 
> Table ‘blog\_development.app\_models’ doesn’t exist
> 
> My question is, how do I tell Rails not to look for a table named  
> “app\_models” AND am I even taking the right approach here? Is there a  
> better way to do this?

You can set self.abstract\_class = true on AppModel to tell rails that  
AppModel is an abstract class with no underlying table.

Fred

---

<div class="post-metadata">

**Author:** ![DanielMedia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@DanielMedia](https://www.ruby-forum.com/u/DanielMedia)\
**Post date:** [September 17, 2010, 9:46pm UTC](https://www.ruby-forum.com/t/rails-base-model/195122/7 "2010-09-17T21:46:55Z")

</div>

DanielMedia wrote:

> Thanks for the help everyone. I really just wanted to have a base  
> model and have the option of overriding methods for all models or  
> adding functionality etc. I will try Fred and MaD’s approach and see  
> how that goes. I also realized that my initial idea to define a  
> to\_slug method in the AppModel is probably wrong and should probably  
> go within a module instead.

Yes, if you even need it. to\_param works wonders.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
