single-table-inheritance

Virgin STI Help

I am working on a horse racing application and I'm trying to utilize STI to model a horse's connections. A horse's connections is comprised of his owner, trainer and jockey. Over time, connections can change for a variety of reasons: The horse is sold to another owner The owner switches trainers or jockey The horse is claimed by a new ...

How to override the attr_protected?

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 fi...

Active Record two belongs_to calls or single table inheritance

In linking a sports event to two teams, at first this seemed to make sense: events - id:integer - integer:home_team_id - integer:away_team_id teams - integer:id - string:name However I am troubled by how I would link that up in the active record model: class Event belongs_to :home_team, :class_name => 'Team', :foreign_ke...

Rails single table inheritance/subclass find condition in parent

Hi all, I have a table called Users (class User < ActiveRecord::Base) and a subclass/STI of it for Clients (class Client < User). Client "filtering" works as expected, in other words Client.find(:all) works to find all the clients. However, for users I need to filter the result to only find users that are NOT clients (where type is nu...

Can nested attributes be used in combination with inheritance?

I have the following classes: Project Person Person > Developer Person > Manager In the Project model I have added the following statements: has_and_belongs_to_many :people accepts_nested_attributes_for :people And of course the appropriate statements in the class Person. How can I add an Developer to a Project through the nested_a...

Two models, one STI and a Validation

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 ST...

STI and polymorphs

Hi, I have problem with my code class Post < ActiveRecord::Base end class NewsArticle < Post has_many :comments, :as => :commentable, :dependent => :destroy, :order => 'created_at' end class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true, :counter_cache => true end And on attempt go get comments for s...

Adding STI to Existing Table...

I want to add STI to an existing table using a custom type column. Let's call this taste_type whose corresponding model is Fruit. In the Fruit model I have: set_inheritance_column :taste_type In my migration to add STI I have: class AddSTI < ActiveRecord::Migration def self.up add_column :fruits, :taste_type, :string, :limit ...

How to return children objects?

I have -- what I think -- is a simple question. Here's my code: class Fruit < ActiveRecord::Base end class Apple < Fruit end class Kiwi < Fruit end Assume that I have all the STI setup correctly, and there are multiple types of Apple and Kiwi records in the table. From here... fruits = Fruit.find(:all) ...how do I return an arr...

Modeling a cellphone bill: should I use single-table inheritance or polymorphic associations?

In my domain: Users have many Bills Bills have many BillItems (and therefore Users have many BillItems through Bills) Every BillItem is one of: Call SMS (text message) MMS (multimedia message) Data Here are the properties of each individual BillItem (some are common): My question is whether I should model this arrangement with s...

using STI and ActiveRecordBase<> with full FindAll

Is it possible to use generic support with single table inheritance, and still be able to FindAll of the base class? As a bonus question, will I be able to use ActiveRecordLinqBase<> as well? I do love those queries. More detail: Say I have the following classes defined: public interface ICompany { int ID { get; set; } string...

Organizing a lot of models that use STI in rails

I have a scenario where I am going to be creating a large number of models that use STI and I'm wondering what the best way to organize this is. I already have other models using STI and I really do not want to add any more files to my models folder. Is there any way to create a folder and add the models using STI there (there could be...

ActiveRecord Validations for Models with has_many, belongs_to associations and STI

I have four models: User Award Badge GameWeek The associations are as follows: User has many awards. Award belongs to user. Badge has many awards. Award belongs to badge. User has many game_weeks. GameWeek belongs to user. GameWeek has many awards. Award belongs to game_week. Thus, user_id, badge_id and game_week_id are foreign...

Rails: Single Table Inheritance and models subdirectories

I have a card-game application which makes use of Single Table Inheritance. I have a class Card, and a database table cards with column type, and a number of subclasses of Card (including class Foo < Card and class Bar < Card, for the sake of argument). As it happens, Foo is a card from the original printing of the game, while Bar is a ...

How do I create the Controller for a Single Table Inheritance in Rails?

I am setting up the Single Table Inheritance, using ContactEvent as the Model that ContactEmail, ContactLetter, and ContactCall will all inherit. But I'm stumped on how to create the routing and the controller. For example, let's say I want to create a new ContactEvent with type Email. I would like a way to do the following: new_cont...

Getting datamapper to ignore 'type' when querying the database

I've got a single table inheritance structure in my application whereby Admins and Mods extends a User class. The table uses a discriminator value so each record has a type of either "admin" or "mod" When it comes to finding a user (on login) I'd like to write the following: current_user = User.find(params[:email => email]) However, ...

How to get the DiscriminatorValue at run time

We have the following classes @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) // optional annotation as this is default @DiscriminatorColumn(name = "apType", discriminatorType = DiscriminatorType.STRING, length = 255) @DiscriminatorValue("AP") public class ApplicationProcess { } And this @Entity @DiscriminatorValue("AP...

validate uniqueness amongst multiple subclasses with Single Table Inheritance

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, :...

Call to `validate` inside a custom validator

I have a model, Card, with multiple subclasses through single table inheritance. I have another model, CardSet, with multiple subclasses through single table inheritance. Cards have many CardSets, and CardSets have many Cards, both through Memberships. Given that a class FooCard is a subclass of Card, and class FooSet is a subclass of...

Rails object inheritence with belongs_to

I have a simple has_many/belongs_to relationship between Report and Chart. The issue I'm having is that my Chart model is a parent that has children. So in my Report model I have class Report < ActiveRecord::Base has_many :charts end And my Chart model is a parent, where Pie, Line, Bar all inherit from Chart. I'm not sure where the...