I’m building a contact management system for a small school and I’m
having trouble coming up with the right data-model that works well with
Here’s the mysql
The contact table contains basic stuff that all contacts will have, and
subsequent models extend Contact to add additional data. Some of the
sub models are Donor, Faculty, Director. A single Contact can be a
Donor, Faculty, and a Director, so it’s what I think is called
multi-table inheritance which isn’t really supported in Rails. Some of
the workarounds I’ve seen seemed overly complex.
I was planning to do something like this:
class Donor < ActiveRecord::Base
for all the sub models and I think that would work, but I can’t use the
syntax Donor.find(1, :include => :contact) because this requires a
donor_id column in contacts, and I doesn’t seem to be good to have
donor_id, faculty_id, director_id, foreign key columns in contacts
(maybe this isn’t a problem??) Also, assuming the above, when I create
one of the sub models I can’t get the contact_id to save to donor
@contact = Contact.new(params[:contact])
@donor = Donor.new(params[:donor])
@donor.contact_id = @contact.id
flash[:notice] = ‘Donor was successfully created.’
redirect_to :action => ‘list’
render :action => ‘new’
I can get the schema above to work, but I think I’ll have to do a lot
of custom sql to make it work rather than using the built-in :include.
Unless there’s a way to “reverse” the underlying join of the has_one
relationship so a contact_id in donors joins to contacts?
Thanks for the help!