[Rails] Single Table Inheritance with self-referential join

Please tell me if this train of thinking makes sense, I need help on
solving this association model problem:

I am doing an app that creates and manages stereoscopic images, one
stereoscopic image is formed by two photos, say photo1 and photo2

The user uploads photo1 and then photo2 and gets a preview if he likes
the preview he/she authorizes it and gets a final stereoscopic image

All this images will be managed via file_column/RMagic plugins

When the user uploads photo1 and photo2 this are placed into an order,
(as when you place an order of fries…)
So there is an Order model that has_one of each:

Photo1 (high-res) belongs_to :order
Photo2 (high-res) belongs_to :order
Preview (low-res) belongs_to :order
Final (high-res) belongs_to :order

Each of Photo1, Photo2, Preview and Final models, need to have
additional user/server-information fields.

I am thinking of implementing this as a “Single Table Inheritance” with
self-referential join modeled like this:

create_table :stereos, :force => true do |t|

t.column :type, :string

common attributes

t.column :photo_name, :string
t.column :created_at, :datetime
t.column :updated_at, :datetime
t.column :order_id, :integer

attributes for type=Photo

t.column :date_taken, :datetime
t.column :pair_id, :integer

attributes for type=Preview

t.column :authorized_by_id, :integer
t.column :photo1_id, :integer
t.column :photo2_id, :integer

attributes for type=Final

t.column :photo1_id, :integer
t.column :photo2_id, :integer
end

Now for the hierarchy of model objects:

class Stereo < ActiveRecord::Base
end

class Photo < Stereo
end

class Preview < Stereo
end

class Final < Stereo
end

So the questions are:

1.- Is this a correct way to solve this relationships modeling problem,
will this work or is there a flow?

2.- photo1 and photo2 allways have to be associated in pairs, am i doing
this correctly via the pair_id field?

3.- preview is the product of merging photo1 and photo2 so i need to
make shure that each preview points to it’s parent via photo1_id and
photo2_id

4.- final is the product of merging photo1 and photo2 so i need to make
shure that each final points to it’s parent via photo1_id and photo2_id

5.- How should I manage field validations, where should I put them?

A stereo image is two almost identical images (one slightly offset)
placed side-by-side such that when viewed properly will produce a 3D
effect. Therefore you might be better off with this hierarchy because
Photo, Preview, and Final are all Images regardless of their source.
Then have a separate Stereo class be the parent relating the 3-types of
images.

class Image < ActiveRecord::Base
belongs_to :stereo

class Photo < Image

class Preview < Image

class Final < Image

class Stereo < ActiveRecord::Base
has_many :images

create_table :images, :force => true do |t|

t.column :type, :string

magic attributes

t.column :type :string # required to activate STI
t.column :created_at, :datetime

common attributes

t.column :stereo_id :integer # the parent
t.column :path :string # file has to be somewhere
t.column :photo_name, :string

attributes for type=Photo

t.column :date_taken, :datetime # irrelevant to the task?
t.column :is_left :boolean #

attributes for type=Preview

attributes for type=Final

end

create_table :stereos, :force => true do |t|

magic attributes

t.column :created_at, :datetime
t.column :updated_at, :datetime

common attributes

t.column :order_id, :integer # An Order has many Stereos

a user actually authorizes a stereo to be finalized

t.column :authorized_by_id, :integer

end

Don’t forget you have to define each class in it’s own model file for
it to work. Also take my advice with a grain of salt as I’ve only been
at this for a week and only found out about STI 2 days ago. :slight_smile: