# Extending a model

**URL:** <https://www.ruby-forum.com/t/extending-a-model/241207>\
**Category:** Rails\
**Created:** [May 19, 2014, 6:18am UTC](https://www.ruby-forum.com/t/extending-a-model/241207 "2014-05-19T06:18:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [May 19, 2014, 6:18am UTC](https://www.ruby-forum.com/t/extending-a-model/241207/1 "2014-05-19T06:18:27Z")

</div>

I’m not sure what is the best approach for my problem. I need a model  
for notification (or alerts) in my application. Notifications are based  
on services, have multiple fields to define conditions and are  
associated  
with a user. Here’s an example for a reddit notification:

user\_id: 1  
type: post\_karma  
condition: above  
value: 100

And here’s a weather notification:

user\_id: 2  
type: temperature  
city: Seattle  
condition: above  
value: 70

So both entries are notifications but they have different fields. Should  
I:

- Create different models for each type of notification?
- Create a base notification model and other models for each type that  
extend the base one?
- Only have 1 model (notification) and then some sort of condition  
table. A notification has multiple conditions.

Thanks.

---

<div class="post-metadata">

**Author:** ![christophe\_n](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5a626/32.png) [@christophe\_n](https://www.ruby-forum.com/u/christophe_n)\
**Post date:** [May 20, 2014, 9:13am UTC](https://www.ruby-forum.com/t/extending-a-model/241207/2 "2014-05-20T09:13:30Z")

</div>

The best advice I can give you here, is one I have read on Sandy Metz  
book([http://www.amazon.co.uk/Practical-Object-Oriented-Design-Ruby-Primer/dp/B00D81W074%3FSubscriptionId%3DAKIAILSHYYTFIVPWUY6Q%26tag%3Dduc08-21%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB00D81W074](http://www.amazon.co.uk/Practical-Object-Oriented-Design-Ruby-Primer/dp/B00D81W074%3FSubscriptionId%3DAKIAILSHYYTFIVPWUY6Q%26tag%3Dduc08-21%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB00D81W074)):

You won’t know what kind of fields all notifications will have in common  
until you implement them all.

First you should implement all the concrete classes for every  
notification  
and then if there is many fields/methods that all of them have in  
common,  
you can create a concern or base notification class.

It’s much easier to refactor from all the concrete to an abstract class,  
then start from a guess abstract class and then refactor to concrete  
ones.  
For example, if you defined a base class notification with 3 fields:  
user\_id, type and value. And after you defined 5 notifications, you  
would  
realise a new one does not need value. And imagine value is used in 3  
different methods. Removing this could be a pain to all the other 4  
“sub-classes”.

So def start by implementing concrete with a good test base and then it  
will be much easier to refactor to an abstract class.

---

<div class="post-metadata">

**Author:** ![christophe\_n](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5a626/32.png) [@christophe\_n](https://www.ruby-forum.com/u/christophe_n)\
**Post date:** [May 21, 2014, 5:31am UTC](https://www.ruby-forum.com/t/extending-a-model/241207/3 "2014-05-21T05:31:20Z")

</div>

I’ll take a look at that book and read on single table inheritance  
(STI). Thanks for your suggestions.

I was also thinking about having a notification model and has N  
attributes. An attribute is a model that basically only have a type and  
value. Think that’s a good idea?

---

<div class="post-metadata">

**Author:** ![christophe\_n](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5a626/32.png) [@christophe\_n](https://www.ruby-forum.com/u/christophe_n)\
**Post date:** [May 20, 2014, 9:20am UTC](https://www.ruby-forum.com/t/extending-a-model/241207/4 "2014-05-20T09:20:53Z")

</div>

On 19 May 2014 05:18, Christophe N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> - Create different models for each type of notification?
> - Create a base notification model and other models for each type that  
> extend the base one?
> - Only have 1 model (notification) and then some sort of condition  
> table. A notification has multiple conditions.

Have you considered STI?

Colin
