single-table-inheritance

Using dynamically created classes in a Single Table Inheritance mechanism

与世无争的帅哥 提交于 2019-12-24 15:52:07
问题 I have an ActiveRecord class called 'DynObject' which can be used for inheritance.. On initialization I dynamically create some Classes that inherit from it: classes_config = { foo: 'foo', bar: 'bar' } classes_config.each do |name,options| klass = Class.new( DynObject ) do end self.klasses[name] = const_set( "#{name.camelize}DynObject", klass ) end This is all good, these classes are created just fine.. But when ActiveRecord tries to load created records the STI mechanism failes..

Mappings are inconsistent with each other. Single Table Inheritance

梦想的初衷 提交于 2019-12-24 11:43:59
问题 I get a lot of mapping errors in Symfony's profiler. I only have this problem with associations with the table inheritance super class. All the other entities and relations are just fine and working perfectly. Errors: ***\ArticleBundle\Entity\Article The mappings ***\ArticleBundle\Entity\Article#buyer and ***\UserBundle\Entity\User#boughtArticles are inconsistent with each other. The association ***\ArticleBundle\Entity\Article#category refers to the inverse side field ***\ArticleBundle

Rails STI and has_many through association not working, SQLException: no such table

陌路散爱 提交于 2019-12-24 08:34:46
问题 I have the following models: class Test < ApplicationRecord end class Exam < Test end class Practice < Test has_many :relations has_many :questions, through: :relations end class Relation < ApplicationRecord belongs_to :practice belongs_to :question end class Question < ApplicationRecord has_many :relations has_many :practices, through: :relations end And this is my schema: create_table "questions", force: :cascade do |t| t.string "title" t.text "text" t.datetime "created_at", null: false t

How do I order by a property that isn't a DB column using Doctrine?

前提是你 提交于 2019-12-24 05:57:17
问题 When defining a relationship, there is a property on the related model (not a DB column), but I would like to sort by it (in the @OrderBy annotation). I have a base model that is extended using single table inheritance. The property in question is basically an order property that is specified in each child class, but is not saved to the DB. (I don't want to add an order column to the DB table, since the ordering depends purely on which child class the discriminator is mapped to. There is

What are the possible solutions to OO/Table inheritance (ie. STI,MTI,CLI) in Rails 5+?

放肆的年华 提交于 2019-12-24 02:44:12
问题 These are the options I see that can help solve "Rails 5 - Object Relation Impedence and how to structure multiple inherited classes/tables" TL;DR - the Object Table Impedance ORM problem. Abstract base class, with each child class having its own table (ie storing the common attributes for each type in its own table) STI, just put everything in one Base class and Table, and create all child attributes in that table, but have the sub-classes (ie your other Rails models) inherit from that Base

validate uniqueness amongst multiple subclasses with Single Table Inheritance

爱⌒轻易说出口 提交于 2019-12-23 04:24:36
问题 I have a Card model that has many CardSets and a CardSet model that has many Cards through a Membership model: class Card < ActiveRecord::Base has_many :memberships has_many :card_sets, :through => :memberships end class Membership < ActiveRecord::Base belongs_to :card belongs_to :card_set validates_uniqueness_of :card_id, :scope => :card_set_id end class CardSet < ActiveRecord::Base has_many :memberships has_many :cards, :through => :memberships validates_presence_of :cards end I also have

DataMapper - Single Table Inheritance

房东的猫 提交于 2019-12-22 11:33:21
问题 Could Some one please explain to me what is going on here? This is an example that I put together to show y'all whats up: class Person include DataMapper::Resource property :id, Serial property :type, Discriminator property :name, String property :age, Integer end class Male < Person end class Father < Male property :job, String end class Son < Male end class Female < Person end class Mother < Female property :favorite_song, String end class Daughter < Female end DataMapper.auto_upgrade! If I

DataMapper - Single Table Inheritance

☆樱花仙子☆ 提交于 2019-12-22 11:32:44
问题 Could Some one please explain to me what is going on here? This is an example that I put together to show y'all whats up: class Person include DataMapper::Resource property :id, Serial property :type, Discriminator property :name, String property :age, Integer end class Male < Person end class Father < Male property :job, String end class Son < Male end class Female < Person end class Mother < Female property :favorite_song, String end class Daughter < Female end DataMapper.auto_upgrade! If I

How to enforce referential integrity on Single Table Inheritance?

半腔热情 提交于 2019-12-22 02:34:33
问题 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

Two models, one STI and a Validation

孤人 提交于 2019-12-21 05:37:32
问题 Let's say I have two tables -- Products and Orders. For the sake of simplicity assume that only one product can be purchased at a time so there is no join table like order_items. So the relationship is that Product has many orders, and Order belongs to product. Therefore, product_id is a fk in the Order table. The product table is STI -- with the subclasses being A, B, C. When the user orders subclass Product C, two special validations must be checked on the Order model fields order_details