Simple table inheritance

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

Andri Mar wrote:
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?

Dont set a default on the width and height columns, so if they aren’t
set they will simply be null. Then simply dont use the width and height
for the other class.

Or if you really want to remove the attributes from your OtherFiles
class:

class OtherFile < Content
undef :width, :height
end
OtherFile.new.width

=> NameError: undefined local variable or method `width’