single-table-inheritance

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

自作多情 提交于 2019-12-05 00:07:41
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 :babies end class Father < Parental has_many :babies end class Baby < ActiveRecord::Base belongs_to :?????? end What should Baby belong_to? In terms of a migration, what should i name/add for foreign key on the babies table? I've had a hard time researching this, is there a definitive source that explains this? The API docs did not seem to hit it on the head OR

How to enforce referential integrity on Single Table Inheritance?

一笑奈何 提交于 2019-12-04 23:57:05
I've read some of Bill Karwin's answers about single table inheritance and think this approach would be good for the setup I am considering: Playlist -------- id AUTO_INCREMENT title TeamPlaylist ------------ id REFERENCES Playlist.id teamId REFERENCES Team.id UserPlaylist ------------ id REFERENCES Playlist.id userId REFERENCES User.id PlaylistVideo ------------- id playlistId REFERENCES Playlist.id videoId REFERENCES Video.id All the CASCADE options are set to DELETE which will work correctly for when a Playlist is deleted, however, what happens if a User or Team is deleted? ie. If a User is

Twig instanceof for inheritance objects

自作多情 提交于 2019-12-04 22:40:22
I am using the following feature from propel http://www.propelorm.org/documentation/09-inheritance.html . I am also using Symfony2 and Twig I have a class structure using the above feature that looks something like this class Event {} class Birthday extends Event {} class Walking extends Event {} now I pass an event object to a twig template and I want to know what type of event it is For instance I want to display an image of a cake if its a birthday and I want to display map routes if its walking event. I cannot use instanceof in Twig as this feature does not exist. Does anyone now why this

How to map collection of each subclass from same hierarchy onto one owning class?

十年热恋 提交于 2019-12-04 17:17:38
Does anyone know how to map a collection of each subclass from the same hierarchy onto one owning class, using JPA 2.0 annotations, backed by Hibernate 4.0.0.Final? Bit convoluted, so here's an example. Hierarchy classes look like this: @Entity @Cacheable(true) @Table(name = "hierarcicals") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "class", discriminatorType = DiscriminatorType.STRING) public abstract class MySuperClass { ... @Entity @DiscriminatorValue("SubClassOne") public class SubClassOne extends MySuperClass { ... @ManyToOne(fetch = FetchType.LAZY)

Get a list/array of child classes from Single Table Inheritance in Rails?

对着背影说爱祢 提交于 2019-12-04 17:10:15
问题 I have a whole bunch of child classes that inherit from a parent class via single-table-inheritance in my Rails app. I'd like a way to get an array of all the child classes that inherit from the main class. I tried the following single-link command that I found in another SO answer, but it only returns the parent class. ObjectSpace.each_object(class<<MyParentClass;self;end) Is there any clean way to do this? EDIT: Apparently Rails only lazy-loads child classes when called in Dev mode, and

Propel Single Table Inheritance Issue

守給你的承諾、 提交于 2019-12-04 17:04:18
I have a table called "talk", which is defined as abstract in my schema.xml file. It generates 4 objects (1 per classkey): Comment, Rating, Review, Checkin It also generates TalkPeer, but I couldn't get it to generate the other 4 peers (CommentPeer, RatingPeer, ReviewPeer, CheckinPeer), so I created them by hand, and made them inherit from TalkPeer.php, which inherits from BaseTalkPeer. I then implemented getOMClass() in each of those peers. The problem is that when I do queries using the 4 peers, they return all 4 types of objects. That is, ReviewPeer will return Visits, Ratings, Comments,

How can I disable a validation and callbacks in a rails STI derived model?

安稳与你 提交于 2019-12-04 16:52:36
问题 Given a model class BaseModel < ActiveRecord::Base validates_presence_of :parent_id before_save :frobnicate_widgets end and a derived model (the underlying database table has a type field - this is simple rails STI) class DerivedModel < BaseModel end DerivedModel will in good OO fashion inherit all the behaviour from BaseModel , including the validates_presence_of :parent_id . I would like to turn the validation off for DerivedModel , and prevent the callback methods from firing, preferably

Can a Discriminator Column be part of the Primary Key in Doctrine2?

本秂侑毒 提交于 2019-12-04 10:30:49
I'm using Single Table Inheritance in Doctrine2 to store OAuth credentials for multiple services. I'd like to use the service's id as the primary key; however, that's not unique across all services. I've setup the database to use the discriminator column and the service's id as the primary key, but I can't find a way to make Doctrine use the discriminator column as the key (in addition to the discriminator column). I'm using docblock annotations, and if I add the discriminator column as an @Id field I get an error: Duplicate definition of column...in a field or discriminator column mapping. If

Rails devise add fields to registration form when having STI

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 21:26:31
Here is my models: class User < ActiveRecord::Base has_one :worker, :class_name => 'Worker', :foreign_key => :worker_id devise :database_authenticatable accepts_nested_attributes_for :worker attr_accessible :worker_id, :email, :password, :password_confirmation, :remember_me, :workers_attributes, :worker_attributes, :name, :worker end class Worker < User devise :database_authenticatable, :registerable belongs_to :user attr_accessible :name, :worker, :workers end I am trying to add the field name to the registretion form at http://localhost:3000/workers/sign_up The sign up form <h2>Create Worker

Hibernate HT_ Temporary Tables ON JOINED inheritance, Migration from Hibernate 3.4.0.GA To 5.1

故事扮演 提交于 2019-12-03 16:14:14
问题 I'm trying to migrate an application from Hibernate 3.4.0.GA to Hibernate 5.1, and after complete the required changes on java code, when I deploy the application I'm watching how Hibernate is trying to create HT_ tables (global temporary), one for each @Inheritance annotated entity. Searching on Google I've found why the tables are being created. But in my case we are not allow to change de database to add new tables. My Inheritance model only has one level of Inheritance and its simple,