I’ve been looking into simple table inheritance and am still a bit
confused. The thing is I have a site that I can upload content to in the
form of files. Now I’ve got to the point where this is my db and models.
–Datatable (migration script)
class CreateContents < ActiveRecord::Migration
def self.up
create_table :contents do |t|
t.column :filename, :string
t.column :mimetype, :string
t.column :url, :string
t.column :filesize, :integer, defautl => 0
t.column :width, :integer, default => 0
t.column :height, :integer
t.column :type, :string
end
end
def self.down
drop_table :contents
end
end
–End
–Classes
class Content < ActiveRecord::Base
end
class Image < Content
end
class OtherFiles < Content
end
–End
Now as you can obviously see the width and height columns are only
applicabl if the content is an Image. Being an old PHP programmer the
way I would do this is that I would check the mime type of the file and
for a collection of mime types I would create an Image class and all
other the OtherFiles class. My question is in short this. How would this
process fit elegantly into the Rails framework. Also is there a way to
tell rails that Image only has witdh and height and others would use the
default value and is it this simple?
–Classes “refactored”
class Content < ActiveRecord::Base
end
class Image < Content
@width
@height
end
class OtherFiles < Content
end
–End