single-table-inheritance

Single Table Inheritance (STI) column associations

五迷三道 提交于 2019-12-12 01:17:52
问题 When using single table inheritance does one have to be careful not to populate the columns which are specific to different models? Is there a way to specify which columns each model uses? 回答1: As far as Rails is concerned, every column can be set in every subclass. You can add logic to your subclass models to prevent certain fields from being set, but there's no automated way to do so. You could probably implement it has a before_save filter. class MySubModel < MyModel UNUSED_FIELDS = %w{

Rails: How to make has_many :through association work with Single Table Inheritance

假如想象 提交于 2019-12-11 08:14:51
问题 So in my current project I have an Article model that can have different kinds of Transactions. It has one main Transaction, but under certain circumstances it can have multiple sub-transactions. Until now I set it up like this: class Article < ActiveRecord::Base has_one :transaction, inverse_of: :article has_many :partial_transactions, through: :transaction, source_type: 'MultipleFixedPriceTransaction', source: 'PartialFixedPriceTransaction', inverse_of: :articles end class Transaction <

How to override the attr_protected?

二次信任 提交于 2019-12-11 03:18:26
问题 I have STI implementation as follows: class Automobile < ActiveRecord::Base end class Car < Automobile end class Truck < Automobile end class User < ActiveRecord::Base has_many :automobiles accepts_nested_attributes_for :automobiles end I am creating a list of automobiles for a user. For each automobile, the UI sets the type field and the properties associated with the automobile.While form submission, the type field is ignored as it is a protected attribute. How do I work around this issue?

Rails STI, subclasses don't show up unless used?

无人久伴 提交于 2019-12-11 01:09:09
问题 I am using Rails STI to model a complex domain. In a view, I am listing all subclasses with: > <% StateDescription.subclasses.each do |state_description| %> <li> > <%= state_description.to_s %> </li> <% end %> With the intent to eventually make each label of the subclass a link to the individual "index" page for just instances of that subclass. This saves me from having to have a list of all possible subclasses myself (and updating it later if I add more). However, I have noticed that only

How to convert entity leaving it id

眉间皱痕 提交于 2019-12-10 20:44:33
问题 There are some entities (Region, Country, City) wich used STI (or even CTI). How it possible convert Country to City leaving old id? 回答1: This is not supported in Doctrine 2 because type casting user objects is not supported by PHP. With that said, Doctrine uses the discriminator column to determine what type of object to hydrate. If you change the value of the discriminator column in the database with a SQL UPDATE, the object type will be changed the next time the object is loaded. This

How to reload files in app/models/subdirectory in dev environment for STI

微笑、不失礼 提交于 2019-12-10 19:07:21
问题 I implemented STI in one of my models, using some tips from Alex Reisner's blog post. I already had all my subclasses use the superclass's controller, with serialize/store to hold the extra attributes. I added the model_name and self.select_options methods to the superclass, and the preload initializer from Alex's blog. I also changed my collection_select in the _form view helper and attribute validation to use the self.select_options method. All my subclasses are in individual files in a app

“IllegalArgumentException occurred calling getter of” while running criteria with SINGLE_TABLE Inheritance strategy

两盒软妹~` 提交于 2019-12-10 13:22:42
问题 I have this error while trying to list objects from database with hibernate Criteria decorated with simple Restrictions Criteria criteria = session.createCriteria(Licence.class); criteria.add(Restrictions.eq("gym", gym.getId())); List<Licence> list = criteria.list(); I have two classes: Licence which has an association to Gym . Those two classes are extending DataModel which is for managing information about data edition - (creation and etition, who and when). What is also important that

Using STI path with same controller

旧时模样 提交于 2019-12-10 11:23:15
问题 I am using STI and am wondering, do I have to have a separate controller for each model? I have a situation where I only use the create and edit actions for one model in the STI relationship, but I get an 'undefined method' error if I try to do a form for. More specifically, I have two models that inherit from List: class RegularList < List class OtherList < List and I have a lists controller that handles these actions, but I only create new models with RegularList using forms. i.e. the only

How to run validations of sub-class in Single Table Inheritance?

£可爱£侵袭症+ 提交于 2019-12-10 01:14:04
问题 In my application, I have a class called Budget. The budget can be of many types.. For instance, let's say that there are two budgets: FlatRateBudget and HourlyRateBudget. Both inherit from the class Budget. This is what I get so far: class Budget < ActiveRecord::Base validates_presence_of :price end class FlatRateBudget < Budget end class HourlyRateBudget < Budget validates_presence_of :quantity end In the console, if I do: b = HourlyRateBudget.new(:price => 10) b.valid? => false b.errors

Propel Single Table Inheritance Issue

大城市里の小女人 提交于 2019-12-09 22:45:01
问题 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