Inheritance: STI? MTI? or a simple polymorphic association?

I am using an application to manage purchase orders. Each order has many processes (stages) including printing and bending. All processes have some common attributes such as quantity and comments, as well as some stage specific:

create_table :stages do |t|
  t.integer :number
  t.decimal :roll_width
  t.decimal :quantity
  t.string :comments
  t.boolean :finished

  t.timestamps
end

create_table :printing_stages do |t|
  t.integer :color
  t.decimal :technical_card

  t.timestamp
end

create_table :bending_stages do |t|
  t.decimal :flap_width
  t.boolean :seal_distance

   t.timestamps
end

      

I'm not sure what the propper approach should be followed here to be able to easily access their general + specific attributes.

If I am using STI I will have a HUGE sigle table as some stages have 10+ specific attributes.

If I use polymorphic associations like:

class Stage < ActiveRecord::Base
  belongs_to :stageable, :polymorphic => true
end

class SlittingStage ActiveRecord::Base
  has_one :stage, :as => stageable
end

class PrintingStage ActiveRecord::Base
  has_one :stage, :as => stageable
end

      

then I would have to access, for example, comments at the print stage such as printing_stage.stage.comments

which is IMHO quite cumbersome and not as elegant as it might be.

Finally, I'm not sure about the implications of using MTI.

Can you give me some advice?

+3


source to share


1 answer


This lady says it better than I can. if you jump for about 20 minutes, that's where your answer lies.



Make a mess of Sandi Metz

+2


source







All Articles