Jump to content

The ultimate community for Ruby on Rails developers.


Extending a model

  • Please log in to reply
1 reply to this topic

#1 Chris911



  • Members
  • 1 posts

Posted 19 May 2014 - 04:24 AM

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 condition 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.


#2 th3mus1cman



  • Members
  • 14 posts
  • LocationRichmond, VA

Posted 23 May 2014 - 07:54 PM

I would probably use Single Table Inheritance (STI) for this. Not the biggest fan of it, but I think in this case it works.  

Rails Developer and Partner at Left + Right

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users