I second Tim’s observation, but wish to add:
From a design perspective, a more generic approach that preserves
everything
you know and love about AR models is to create the following
tables/models:
Category
id
name
description
abbreviation
SystemType
id
category_id
name
description
abbreviation
Something like that. You can make SytemType acts_as_list if you like to
order them, and categories can acts_as_tree if you want them to be
hierarchically related. Knock yourself out. Can be as simple or complex
as
you wish to make it.
Category will have a one-to-many relationship with SystemType.
Where you would otherwise be tempted to add a table, instead add a
Category
record. When adding a new SystemType, you must relate it to a Category.
The
type system can grow without needing migrations.
You can also add static methods to SystemType to return all types for a
given category. Let’s say one of those is ‘Image’ (denoting an image
type),
inside SystemType you can do something like this:
def self.find_all_by_category( category_name )
category = Category.find_by_name category_name
if category
types = SystemType.find( :all,
:conditions => [“category_id =
?”,
category.id],
:order => ‘name’ ) # this can
be
whatever you want to order by
#
and is
optional if you don’t care about ordering
return types if types.count > 0
end
[]
end
def method_missing( method, *args, &block )
result = self.find_all_by_category method.to_s
return result if result
super(method,args,block)
end
Now you can do something like SystemType.Image to return all images. Of
course, you may have to follow category naming conventions and/or
accommodate for stuff like spaces or capitalization, or avoid the
complexities of a method_missing implementation altogether and simply
name
each category in your SystemType model:
def self.images
return self.find_by_category ‘Image’
end
This is perhaps a bit more idiomatic Ruby: SystemType.images.
Whatever works for you. The main point is to have one model to store all
system types by category. You can order by whatever you want, or not at
all,
in a standardized way, or extend find_by_category to optionally provide
a
Proc object that does the sorting logic on a per-call basis.