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
35
votes
3 answers

Prevent STI when inheriting from an ActiveRecord model

On Rails 3.2.6, I have a class that inherits from ActiveRecord::Base: class Section < ActiveRecord::Base ... end When I inherit from this class, Rails will assume I want STI: class AnotherSection < Section ..Rails assumes I have a type field,…
Mike
  • 2,103
  • 3
  • 18
  • 24
32
votes
1 answer

Testing simple STI with FactoryGirl

I have got a class, that is the base of some other classes that specializes the behavior: class Task < ActiveRecord::Base attr_accessible :type, :name, :command validates_presence_of :type, :name, :command # some methods I would like to…
Mark
  • 7,507
  • 12
  • 52
  • 88
19
votes
6 answers

Rails STI: How to change mapping between class name & value of the 'type' column

Because of company rules I can't use our domain class names; I am going to use an analogy instead. I have a table called projects which has a column called 'type' with possible values as 'indoor' & 'outdoor'. Records having indoor and outdoor have…
arun
  • 193
  • 1
  • 1
  • 4
17
votes
3 answers

Multi Table Inheritance with rails 3

Are there standards or best practices yet when it comes to multi table inheritance in rails 3? So far the best article I could find was: http://mediumexposure.com/multiple-table-inheritance-active-record/ But even that needed some changes(e.g.…
jtesch
  • 778
  • 1
  • 7
  • 9
16
votes
4 answers

STI and form_for problem

I am using Single Table Inheritance for managing different types of projects. Models: class Project < ActiveRecord::Base end class SiteDesign < Project end class TechDesign < Project end Edit action from projects_controller: def edit @project…
ebsbk
  • 4,512
  • 3
  • 24
  • 29
15
votes
2 answers

Implementing Abstract Base Model Class, the Rails Way™

I have a Book and Download model that share many attributes, so my goal is to inherit the common attributes from a DownloadableResource model. Had a look at STI, but I went the abstract base model class way instead: models: class…
Marius Butuc
  • 17,781
  • 22
  • 77
  • 111
14
votes
3 answers

Rails Question: belongs_to with STI -- how do i do this correctly?

I've been playing around with STI and belongs_to / has_many relationships and I'm a bit confused. I have a few questions based on a model configuration similar to: class Parental < ActiveRecord::Base end class Mother < Parental has_many…
11
votes
2 answers

How to always use the base class and ignore STI in Rails?

I have a module that I'm including in several models with this content: self.class.find_by_foo(bar) Everything was fine until I started using STI. That line should always generate the query select * from table where foo=bar" and not select * from…
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
11
votes
2 answers

ActiveRecord STI: How can I break out of the parent class' default scope

On Rails 3.1 RC6, given class Animal < ActiveRecord::Base default_scope where(legs: 4) end The following does not work as expected: class Man < Animal default_scope unscoped.where(legs: 2) end The resulting SQL statement looks like…
Prathan Thananart
  • 4,007
  • 3
  • 19
  • 18
11
votes
2 answers

Rails form_for that uses STI base class

I've got a pretty simple (I think) single-table inheritance (STI) setup in my Rails app. There is a User model with a nested resource Post. Using STI, I have it so that some Posts can be Post::Urgent objects. I noticed that my URL helpers like <%=…
isthmuses
  • 1,316
  • 1
  • 17
  • 27
10
votes
2 answers

ActiveRecord: query not using correct type condition for STI subclass

I have a set of STI subclasses inheriting from a User base class. I am finding that under certain conditions inside a subclass' definition, queries on the subclasses do not correctly use the type condition. class User < ActiveRecord::Base #…
9
votes
2 answers

devise and STI how to login as Base class upon registration

I've seen similar posts already, but couldn't quite get the answer I needed. I have a User model and using STI a Student model that is a type of User. When I create a new Student, Devise logs in that Student with a student_session. The problem is…
alassiter
  • 505
  • 1
  • 5
  • 15
9
votes
1 answer

Can a foreign key have a constant instead of a field name? Relate FK to STI subclass

Setup So here's a scenario which I'm finding is rather common once you decide to play with STI (Single Table Inheritance). You have some base type with various subtypes. Person < (Teacher,Student,Staff,etc) User < (Member,Admin) Member <…
Shadow Radiance
  • 1,349
  • 13
  • 20
9
votes
2 answers

HABTM association associated to single table inheritance

I have a product model that has many sections, and a section can belong to many products. The section model has subclasses of Feature, Standard and Option. My models are: class Product < ActiveRecord::Base has_and_belongs_to_many :categories …
rick
8
votes
2 answers

performant ordering of keys in a MySQL compound index (WRT Rails Polymorphic associations and STI)

Previously, I asked this question about compound indexes on polymorphic foreign keys in ActiveRecord. The basis of my question was my understanding that indexes should be based on the cardinality of your column, and there's generally pretty low…
jmaxyz
  • 928
  • 1
  • 8
  • 19
1
2 3
25 26