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

How to form a polymorphic has_many on an inherited class

In a Rails 4.2 app I have a model that inherits from Draftsman. module ActsAs class Draft < Draftsman::Draft include ActsAs::Votable end end Users can vote to approve/reject edits, and the ActsAs::Votable mixin adds this polymorphic…
Andy Harvey
  • 12,333
  • 17
  • 93
  • 185
2
votes
0 answers

Rails define association to STI subclass with class_name option

Slightly simplified, I have the following code: class MediaFile < ActiveRecord::Base has_one :default_service, ->(m){where(type: _get_service_type(m)))}, :class_name => "Service" #... end where Service is an STI model with a few different…
2
votes
1 answer

Use Rails STI model with join conditions

I have the following models defined: class Group < ActiveRecord::Base end class Person < ActiveRecord::Base end class Policeman < Person end class Firefighter < Person end Inside Group, I would like to get all groups that have Policemen, for…
linkyndy
  • 17,038
  • 20
  • 114
  • 194
2
votes
1 answer

Single table inheritance and changing type won't save

I have a STI model where the subclasses of each use different validations. The Rails defaults will run the original type's validations at runtime, so I am trying to use "becomes" to force the validations of the new type. My code looks like…
Dawn Green
  • 483
  • 4
  • 16
2
votes
2 answers

How to build a select menu using STI types in rails with simple_form

I'm new to STI and I'm trying to build a select menu that lists all the STI types in a Model. I am using simple_form and Rails 4.1. How do I get a drop down menu with the StiRecord.types? I'd like to store the type in the database in a string. def…
2
votes
0 answers

How to store the name of child class in audits "auditable_type" column in single table inheritance

I am using single table inheritance in rails, and I am auditing the parent class. Whenever, I create/update the child class/model, auditable type for that audit stores the name of the parent class instead of the name of the child class . Is this…
Ankush
  • 146
  • 8
2
votes
1 answer

Rails: belongs_to association not returning the correct object

I've got an Order model with two belongs_to associations, each to a different sub-class of my Account model. After loading an order from the database, both associations point to the same model, even though the foreign keys are correct. class…
Terry G Lorber
  • 2,932
  • 2
  • 23
  • 33
2
votes
2 answers

Rails 4: single-table inheritance and NOT using class name as "type"

I have several items that I want to lump into a single lookups table instead of having 20+ separate tables, each with the same attributes: countries states email_types phone_types address_types etc... In Rails I just add the type column to the…
Dan L
  • 4,319
  • 5
  • 41
  • 74
2
votes
1 answer

Rails 4 - STI has_many :through with polymorphic association

I'm have an issue trying to combine Rails 4, STI, polymorphic associations with has_many: :through The issue: UserConnection is not saving the correct userable_type according to the STI class UserConnection < ActiveRecord::Base belongs_to…
2
votes
2 answers

Rails 4 STI inheritance

I'm using single table inheritance successfully like so: class Transaction < ActiveRecord::Base belongs_to :order end class Purchase < Transaction end class Refund < Transaction end The abbreviated/simplified PurchaseController looks like…
EasyCo
  • 2,036
  • 20
  • 39
2
votes
0 answers

Ruby Rails STI Controllers and test unit routes

I am using STI on my ruby on rails project and I am trying to create tests for controllers. Example: class Animal end class Lion < Animal end The specific is that i am trying to disable Animal routes completly ( except index ), so i can only use…
AlexJP
  • 49
  • 4
2
votes
1 answer

Waterline default query condition

How can I set the default where/sort condition in my SailsJS waterline model? In Rails I would use default scope.
xpepermint
  • 35,055
  • 30
  • 109
  • 163
2
votes
0 answers

Eager loading an association of a subtype while retrieving a common set with multiple subtypes

Here's what I have: class Element < ActiveRecord::Base; end class Asset < ActiveRecord::Base; end class Text < Element; end class Graphic < Element has_and_belongs_to_many :assets end Element.includes(:assets).all This gives me an…
elhoyos
  • 849
  • 9
  • 19
2
votes
2 answers

Rails single table inheritance. Shared controller, howto Update (CRUD)

I've got three classes Admin, Client, Agent which all inherit from User < ActiveRecord::Base. The system is designed using STI, hence all classes share the same users table. I am interested in keeping the CRUD functionality pretty much the same,…
lllllll
  • 4,715
  • 6
  • 29
  • 42
2
votes
4 answers

Sign in inherited user after registration with Devise

I am using rails 3.2.14 and devise 3.2.1, and I have an "Admin" with an STI relationship with my devise "User" model. When I register a new Admin with the form below, I get the following error: undefined method `new_admin_session_path' for…
pingu
  • 8,719
  • 12
  • 50
  • 84