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
8
votes
4 answers

STI in Rails: How do I change from a superclass to a subclass without accessing the "type" attribute directly?

So, I have the following: class Product < ActiveRecord::Base # Has a bunch of common stuff about assembly hierarchy, etc end class SpecializedProduct < Product # Has some special stuff that a "Product" can not do! end There's a manufacturing…
Jamie
  • 93
  • 1
  • 4
7
votes
4 answers

rails semi-complex STI with ancestry data model planning the routes and controllers

I'm trying to figure out the best way to manage my controller(s) and models for a particular use case. I'm building a review system where a User may build a review of several distinct types with a Polymorphic Reviewable. Country (has_many reviews &…
ere
  • 1,739
  • 3
  • 19
  • 41
7
votes
0 answers

ActiveRecord::AssociationTypeMismatch on STI through association

It's gonna get a bit tricky. So, I have a forum app consisting of Section entities. And the forum also has Char entities (who write posts and create topics) and CharGroup entities for grouping chars. The sections can be set to private access. Then…
Almaron
  • 4,127
  • 6
  • 26
  • 48
7
votes
2 answers

Routes, path helpers and STI in Rails 4.0

This is driving me crazy! I have the two models Lion and Cheetah. Both inherit from Wildcat. class Wildcat < ActiveRecord::Base; end class Lion < Wildcat; end class Cheetah < Wildcat; end STI is used here. They all get handled through the…
Hiasinho
  • 656
  • 6
  • 13
7
votes
4 answers

Rails STI association with subclasses

I'm getting some strange behaviour when fetching collections from a has_many association with rails 3 when using STI. I have: class Branch < ActiveRecord::Base has_many :employees, class_name: 'User::Employee' has_many :admins, class_name:…
Tom Livesey
  • 401
  • 1
  • 4
  • 10
6
votes
2 answers

Rails STI and the setting of the "type" string

I think I need to use STI in Rails. Here's my class: class Person < ActiveRecord::Base end class Landlord < Person end and the people table has a :type column that's a string. So, what I expect is to see in the table, is that every row that is a…
TheDelChop
  • 7,938
  • 4
  • 48
  • 70
6
votes
2 answers

Rails: PolyMorphic or STI or something else for User management?

I've been banging my head against a wall trying to wrap my head around this, so any guidance would be much appreciated... I want to have a User system setup to reflect the following hierarchy: User |- email address |- password |- billing…
neezer
  • 19,720
  • 33
  • 121
  • 220
6
votes
3 answers

Rails 5 - Object Relation Impedence and how to structure multiple inherited classes/tables

EDIT I have edited this from the original to make it easier to understand. I understand the Object Relationship Impedance problem. I understand Rails STI and Polymorphism (the Rails way and that it's not true OO Polymorphism). I've read a TON of…
rmcsharry
  • 5,363
  • 6
  • 65
  • 108
6
votes
3 answers

Active Record includes with STI

I have the following model class Event < ActiveRecord::Base has_many :attendances class Attendance < ActiveRecord::Base belongs_to :user class Student < User has_one :student_detail class StudentDetail < ActiveRecord::Base belongs_to…
dboyd68
  • 1,104
  • 15
  • 33
6
votes
2 answers

Specify unique attributes for child models in rails using STI

I plan to use STI in Rails with the following models: class Promo < ActiveRecord::Base end class Event < Promo end class Discount < Promo end There are just a couple of differences in terms of attributes between Event and Discount so I figure STI…
kubasub
  • 455
  • 1
  • 5
  • 12
6
votes
7 answers

Rails STI and strong parameters

I have a Client resource with 2 types: Person and Company. routes.rb: resources :clients resources :people, :controller => "clients", :type => "Person" resources :companies, :controller => "clients", :type => "Company" clients_controller: def new …
Bogdan Popa
  • 1,099
  • 1
  • 16
  • 37
6
votes
1 answer

Of STI, MTI, or CTI, which is the currently suggested Rails 4 solution?

I have the a basic events table, and want to have sub-tables for each event type (hiking, party, riverrun, etc). I see a lot of old (2011/2012) posts regarding CTI, MTI and STI. Some solutions worked for Heroku, while others did not. What is the…
6
votes
3 answers

Using scope, defined in parent model, inside it's child (STI pattern)

I implement a class hierarchy using STI pattern class A scope :aaa, where([someField]:[someValue]) end class B < A end The problem is that when I try to call something like: B.limit(5).aaa => SELECT "[table]".* FROM "[table]" WHERE…
AntonTkachov
  • 1,784
  • 1
  • 10
  • 29
5
votes
1 answer

Sudden "uninitialized constant PurIssue" error on STI_preload while upgrading rails to 7.0.1

I am in the process of upgrading a ~2 year old Rails app from 6.1 to 7.0.1 I have an STI setup where Pursuit is the main class and it has several other types as descendants: # app/models/pursuit.rb require 'sti_preload' class Pursuit <…
5
votes
0 answers

Update an entity type in Single Table Inheritance

In the Single Table Inheritance Scenario, is there any way to to update a saved BaseEntity object in database as an InheritedEntity object? Please consider the following scenario: @Entity() @TableInheritance({column: {name: 'type', type:…
Emech
  • 611
  • 1
  • 4
  • 16
1
2
3
25 26