[Rails] Authentication design/ideas

Francois Beausoleil francois.beausoleil at gmail.com
Tue Jul 25 14:39:27 GMT 2006


Hello Jord,

2006/7/25, Jordan Elver <jordan.elver at gmail.com>:
> Is this a use for STI? Can I somehow alias the User model so that it
> can be accessed as Customer as well? Shall I just forget this and live
> with the name User? :)

Nothing prevents you from doing:

class Order < ActiveRecord::Base
  belongs_to :customer, :class_name => 'User', :foreign_key => :customer_id
end

class User < ActiveRecord::Base
  has_many :orders, :foreign_key => :customer_id
end

That takes care of one problem.  Secondly, your admin users.  Yes,
this is a use for STI.  You can do this:

class User < ActiveRecord::Base
  validates_presence_of :login, :password
end

class Customer < User
  validates_presence_of :address, :phone
  has_many :orders, :foreign_key => :customer_id, :order => :order_no
end

class Order < ActiveRecord::Base
  belongs_to :customer, :foreign_key => :customer_id
  validates_presence_of :customer_id
end

Hope that helps !
-- 
François Beausoleil
http://blog.teksol.info/


More information about the Rails mailing list