All, I've a question concerning STI in Rails. Let's say I've a Person class and two classes that inherit from Person: User and Customer. In the DB I've the people table to store both instances of User and Customer. Now, let's say I also have Address class, which references a Customer, and each customer has many addresses. How should I name the relations? Can it be done in the simple way without specifying column names? If it can, then what should be the column names? Ex. class Customer < Person has_many :addresses end class Address < ActiveRecord::Base belongs_to :customer end Now, if I call the referencing column in addresses table customer_id, it won't find the people table and if I call it person_id, then Customer.find(:first).addresses will generate bad query, because addresses table doesn't have customer_id. So, should I put all the relations in base class (Person) to make it work, and just ignore the fact that User doesn't have addresses? Or is there any other way to solve it?
on 2006-04-03 16:09
on 2006-04-05 19:57
Try using a "person_id" column in the "addresses" table and specifying the column name in the code. Like this: class Customer < Person has_many :addresses, :foreign_key => 'person_id' end class Address < ActiveRecord::Base belongs_to :customer end Hope that works. Ryan