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.

- Bob

On Tue, May 18, 2010 at 11:15 AM, Tim Shaffer <[email protected]> wrote:

> > The problem is, when you want to go back and
> > alphabetize the categories list, for example, you get the record id's
> > changing, and then an object with a category_id of 12 is now pointing
> > to a different category than it was before (since you reordered the
> > table), which forces you to create after_save actions to update all
> > the objects that reference the categories table.
>
> What? I think you might be doing it wrong. You don't need to change
> anything in the database to show the categories in alphabetical order:
>
> Category.find(:all, :order => 'title')
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rubyonrails-talk%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to