Questions tagged [sti]

Single Table Inheritance - a mechanism to add the principle of object-oriented inheritance onto relational database models by having child classes map onto the same table as their ancestors.

In Single Table Inheritance, types are discriminated by a field in the table that indicates what class in the hierarchy the object belongs to.

For more information see:

Single Table Inheritance - Martin Fowler - Patterns of Enterprise Application Architecture

384 questions
5
votes
1 answer

How many classes is too many? Rails STI

I am working on a very large Rails application. We initially did not use much inheritance, but we have had some eye opening experiences from a consultant and are looking to refactor some of our models. We have the following pattern a lot in our…
Alan Peabody
  • 3,507
  • 1
  • 22
  • 26
5
votes
2 answers

Rails STI override model_name in parent class for all subclasses

I am using STI in a Rails app and in order to not have to define routes for all subclasses, I put the following in each subclass: def self.model_name Mapping.model_name end In the above example, Mapping is the parent model name. Example: class…
Adam
  • 673
  • 1
  • 8
  • 18
5
votes
1 answer

Combine STI and polymorphic with rails 4

I'm running with some issues trying to combine STI and polymorphic associations with the following structure : class User < ActiveRecord::Base end class Runner < User has_many :subscriptions, as: :subscriber, :dependent => :destroy has_many…
Axel Manzano
  • 87
  • 2
  • 11
5
votes
1 answer

Design of Rails app with multiple user types using Devise and STI

I'm relatively new to Rails, having written only one app so far. I used Devise for authentication in that app. Now I'm on to my second one, and I have to put more thought into authentication since I have many user types, as opposed to the single…
andreobrown
  • 156
  • 2
  • 10
5
votes
2 answers

How to build ActiveRecord associations with STI

I'm having problems with AR trying to build associations of models that inherit from others. The problem is the associated models are being saved to the database before the call do the save method. I found more information in this page…
Gus
  • 942
  • 9
  • 32
5
votes
1 answer

How to combine different classes from an STI table into a single result set using ActiveRecord?

We're building an application that creates group pages similar to Facebook's group pages. Someone can post to a page and that post can have replies. Since they have very similar properties, the posts and the replies are combined into the same STI…
Peter Nixey
  • 16,187
  • 14
  • 79
  • 133
5
votes
1 answer

Deprecation warning with STI model_name workaround

In my Single Table Inheritance models, I am overriding the inherited method in the base model so that all descendent models are recognized by the base model's name. The following code is used to add an override to the model_name method for all…
Dave Isaacs
  • 4,499
  • 6
  • 31
  • 44
4
votes
1 answer

Is this a legitimate use of Rails' Single Table Inheritance?

I've just been reading Chad Fowler's blog post about 20 Rails Development No-Nos. On Single Table Inheritance he comments: The storage of a column called “type” which holds a class name is a pretty good indicator that something fishy is going on.…
John Topley
  • 113,588
  • 46
  • 195
  • 237
4
votes
1 answer

Subclassing activerecord and maintain subclass after db fetch

I have an ActiveRecord model Media which is supposed to be able to store similarly structured information about different types of media (Media::Book, Media::Movie, Media::Music). However each of these subclasses has unique methods. # TABLE medias #…
user545139
  • 935
  • 11
  • 27
4
votes
2 answers

Single Table Inheritance and routing in Ruby on Rails 3.0

I am having some trouble getting routing to play nicely with Single Table Inheritance in my Ruby on Rails application. I am using Ruby 1.9.2 and Rails 3.0.6. This is in development so the back-end is SQLite3, in case that makes a difference. Let's…
Ion the Geek
  • 41
  • 1
  • 3
4
votes
2 answers

Why does rails not respect the type of a belongs_to associated object with STI, when its superclass is abstract?

I've come across this rather odd bit of behaviour in a rails application I'm working on. I have multiple types of Post in an inheritance heirarchy, and a Post has_many FeedEntries. class Post < ActiveRecord::Base has_many…
mistertim
  • 5,123
  • 4
  • 20
  • 27
4
votes
2 answers

Achieve Single Table Inheritance with Sequelize

Is there a way to use sequelize to create single table inheritance? I would like to have a STI for a Purchase and PartialPurchase model where I would have a type field which would be "Purchase" or "PartialPurchase" and classes Purchase and…
unflores
  • 1,764
  • 2
  • 15
  • 35
4
votes
1 answer

Polymorphic relationship vs STI vs Class table inheritance with RoR

There are three types of invoice items with following tables 1) SubscriptionItems, 2) Prorations, 3) UsageItems, Those have the same attributes below invoice_id amount stripe_invoie_id However only SubscriptionItem and Proration…
Toshi
  • 6,012
  • 8
  • 35
  • 58
4
votes
4 answers

Rails: Is this a use case for Single Table Inheritance (STI)?

Consider this setup. Please understand that our setup is much more detailed but this is a simple example. competition which has name. This is an annual competition. competition_instances which has location, starts_at. Each competition has sports…
csi
  • 9,018
  • 8
  • 61
  • 81
4
votes
1 answer

rails 4: polymorphic set base class type instead of inherited

I have problem with rails relations. I have base model ant his inherited version class User < ActiveRecord::Base end class Admin < User end Next I have membership model with polymorphic association class Membership < ActiveRecord::Base …
betromatic
  • 69
  • 4
1 2
3
25 26