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
4
votes
1 answer

rails - Polymorphic Association or Single-Table Inheritance

I am torn between STI, polymorphic association, or a types table. I already have a units table with following fields: name account_id speed_limit is_speeding activation_state unit_status_id There are actually three different kinds of units: gps…
JohnMerlino
  • 3,900
  • 4
  • 57
  • 89
4
votes
6 answers

Rails Single table inheritance problem

I'm trying to setup single table inheritance in my Rails app for a User model and its subclasses Member, Subscriber, and Staff. I have a model file for each: user.rb, member.rb, etc The user model is defined: class User < ActiveRecord::Base; end; I…
DiegoSalazar
  • 13,361
  • 2
  • 38
  • 55
4
votes
1 answer

rails change path of partial collection rendering

I have an STI relationship where a conversation is composed of both messages and images. Now when I go to render them i use: <%= render conversation %> which works perfect. It finds the given template for the given object and renders it. Now for my…
Mike Silvis
  • 1,299
  • 2
  • 17
  • 30
4
votes
2 answers

Deadlocks when concurrent editing a closure tree hierarchy

How can I avoid database deadlocks when using closure_tree to concurrently manipulate a set of models with common attributes on a hierarchical structure? They present in the following flavors: When issuing an #append/prepend_sibling Mysql2::Error:…
4
votes
1 answer

Rails STI overriding scopes

Let's say I have a STI setup as follows: class User < ActiveRecord::Base scope :busy, -> { where('busy_factor > 1') } end class HeroUser < User scope :busy, -> { where('busy_factor > 5') } end So, hero users have a different threshold for the…
Aaron Gibralter
  • 4,773
  • 3
  • 35
  • 50
4
votes
2 answers

Single Table inheritance with CRUD and forms in Rails

I'm a bit confused about STI in rails. My situation: I have a Contact model that has description and data string fields, to store some contact like phone, fax, email, etc. Now when I have some specific contact type like phone number of email address…
Uko
  • 13,134
  • 6
  • 58
  • 106
3
votes
2 answers

Where do indexes go when using STI?

I am using Rails and postgres. I have a couple of models using STI and i was wondering where i should put indexes on the tables and why? For example lets say i have the following setup: class Comment < AR; end class MovieComment < Comment;…
3
votes
2 answers

Rails Sti: single path, different controller

Have STI classes: class Page < ActiveRecord::Base belongs_to :user end class FirstTypePage < Page end class SecondTypePage < Page end Controllers for each class, class PageController < AplicationCorroller end class FirstTypePageController <…
potapuff
  • 1,839
  • 4
  • 18
  • 36
3
votes
2 answers

ActiveRecord Inheritance with Different Database Tables

I have just started investigating using more advanced models in Rails. One that I use regularly, with great success, is model where a many-to-many cross-reference relationship is accessed by a class that itself is a sub-class of the base class in…
Larry M
3
votes
1 answer

Rails 3: Should I use STI or just an extra column? (seeking advice)

I am working on a project (Rails 3.0.3) where I think I may need to use STI, but I am not sure if I should just add an extra column to a table and be done with it. In my object model, (for a gaming system) I have players (who belong to agencies) and…
3
votes
2 answers

How to model "appointments" in Rails?

Here's the scenario I'm facing: An appointment could be scheduled for: today some time during the week on a specific date So, the attributes can be different for each "type" of appointment. I was thinking of these models and using it with STI, but…
Zabba
  • 64,285
  • 47
  • 179
  • 207
3
votes
1 answer

Rails STI conditional sub-classing from base class

I'm developing a project where I have an entity which may have two kinds of assets: Pictures and Videos, basically. Since I want all the assets to be on the same table and a single upload form for either Pictures or Videos, I'm using Single Table…
punnie
  • 2,404
  • 4
  • 17
  • 32
3
votes
1 answer

Can I use ActiveRecord relationships with fields from an Hstore?

Can I tie a model to another through active record belongs_to using a field from an hstore hash? I'll elaborate: I have a User model that gets subclassed via STI on one of its fields to many different other User models based on permissions: class…
Alfredo Gallegos
  • 613
  • 6
  • 22
3
votes
3 answers

Rails STI class auto initialize

I'm trying to make a STI Base model which changes automatically to inherited class like that: #models/source/base.rb class Source::Base < ActiveRecord::Base after_initialize :detect_type private def detect_type if (/(rss)$/ ~= self.url) …
pvf
  • 82
  • 1
  • 6
3
votes
1 answer

rails carrierwave generated url for STI

I have 1 STI class inherits from User class class User < ActiveRecord::Base mount_uploader :avatar, AvatarUploader end class Staff < User end but when I looping all the staffs user including the image url like this: controller @staffs =…
Kris MP
  • 2,305
  • 6
  • 26
  • 38