# Rails base model

**URL:** <https://discuss.rubyonrails.org/t/rails-base-model/54425>\
**Category:** rubyonrails-talk\
**Created:** [September 17, 2010, 1:12am UTC](https://discuss.rubyonrails.org/t/rails-base-model/54425 "2010-09-17T01:12:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielMedia1](https://avatars.discourse-cdn.com/v4/letter/d/bbe5ce/32.png) [@DanielMedia1](https://discuss.rubyonrails.org/u/DanielMedia1)\
**Post date:** [September 17, 2010, 1:12am UTC](https://discuss.rubyonrails.org/t/rails-base-model/54425/1 "2010-09-17T01:12:26Z")

</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:** ![Jon\_Leighton](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@Jon\_Leighton](https://discuss.rubyonrails.org/u/Jon_Leighton)\
**Post date:** [September 17, 2010, 8:08am UTC](https://discuss.rubyonrails.org/t/rails-base-model/54425/2 "2010-09-17T08:08:06Z")

</div>

Hi there,

> 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?

Short answer: Use set\_table\_name \<[ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-set_table_name&gt); 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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 17, 2010, 9:01am UTC](https://discuss.rubyonrails.org/t/rails-base-model/54425/3 "2010-09-17T09: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.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [September 17, 2010, 10:08am UTC](https://discuss.rubyonrails.org/t/rails-base-model/54425/4 "2010-09-17T10:08:20Z")

</div>

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:** ![MaD](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@MaD](https://discuss.rubyonrails.org/u/MaD)\
**Post date:** [September 17, 2010, 10:21am UTC](https://discuss.rubyonrails.org/t/rails-base-model/54425/5 "2010-09-17T10:21:59Z")

</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 \<[ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-se...&gt); in Post.

the right (short) answer is:

class AppModel \< ActiveRecord::Base &nbsp;&nbsp;# define as abstract. &nbsp;&nbsp;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:** ![DanielMedia1](https://avatars.discourse-cdn.com/v4/letter/d/bbe5ce/32.png) [@DanielMedia1](https://discuss.rubyonrails.org/u/DanielMedia1)\
**Post date:** [September 17, 2010, 7:00pm UTC](https://discuss.rubyonrails.org/t/rails-base-model/54425/6 "2010-09-17T19:00:21Z")

</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:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 17, 2010, 7:46pm UTC](https://discuss.rubyonrails.org/t/rails-base-model/54425/7 "2010-09-17T19: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,
