Design question

pepe
Tue, 18 May 2010 12:58:59 -0000

I''m working on application where several tables
(users,customers,other...) will have the same name structure (prefix,
first, middle, last, suffix).

I have thought about factoring out the name to a separate model:

*** Migrations ***
class CreateNames < ActiveRecord::Migration
  def self.up
    create_table :names do |t|
      t.string :prefix
      t.string :first
      t.string :middle
      t.string :suffix
    end
  end
end

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.integer   :name_id
      # other columns here
  end
end

class CreateCustomers < ActiveRecord::Migration
  def self.up
    create_table :customers do |t|
      t.integer   :name_id
      # other columns here
  end
end


*** Models ***
class Name < ActiveRecord::Base
  has_one :user
  has_one :customer
end

class User < ActiveRecord::Base
  belongs_to :name
end

class Customer < ActiveRecord::Base
  belongs_to :name
end

I believe the concept is sound but I am concerned about a couple of
things. I know that search capabilities by user/customer/other are
coming and name searches are going to be in the mix.

Am I putting myself into a bad situation by going this way?
Are named scopes going to be my friends?
What would be the best/fastest way to query the database with this
design?
Am I abstracting the DB too much?

Thanks

Pepe