Rails:Polymorphic User Table是一个使用AuthLogic的好主意吗?

时间:2010-05-26 03:48:40

标签: ruby-on-rails authlogic polymorphic-associations

我有一个系统,我需要从主页上的一个登录表单中登录三种用户类型:客户,公司和供应商。

我创建了一个根据AuthLogic在http://github.com/binarylogic/authlogic_example的示例应用程序运行的用户表。我添加了一个名为“用户类型”的字段,该字段当前包含“客户”,“公司”或“供应商”。

注意:每个用户类型包含许多不同的字段,因此我不确定单表继承是否是最好的方法(如果此结论无效,则欢迎更正)。

这是一个多态关联,其中三种类型中的每一种都用用户记录“标记”了吗?我的模型应该如何显示,以便我的用户表和我的用户类型客户,公司,供应商之间有正确的关系?

非常感谢!

------ ------ UPDATE

当前设置:

#User model
class User < ActiveRecord::Base
  acts_as_authentic
  belongs_to :authenticable, :polymorphic => true
end

#Customer model (Company and Vendor models are similar)
class Customer < ActiveRecord::Base
  has_many :users, :as => :authenticable
  acts_as_authentic
end

这是设置它们的有效方法吗?

1 个答案:

答案 0 :(得分:0)

这听起来像是你要做的事情:

users可以是groups中的一个user。如果这不太正确,那么一点澄清会有所帮助。

由于AuthLogic只关心它的特殊字段,因此在用户模型中制作和使用其他字段并不重要。

每个#The Authlogic Fields t.string :username, :null => false t.string :crypted_password, :null => false t.string :password_salt, :null => false t.string :persistence_token, :null => false #Your field to identify user type t.integer :user_type #If your user is a customer, you would populate these fields t.integer :customer_name ... #If your user is a company, you would populate these fields t.integer :company_name ... #If your user is a vendor, you would populate these fields t.integer :vendor_name ... 可能都是这样的:

vendor

我不确定“单表继承”是什么意思,但是如果你想在一个与用户表分开的表中保存class User < ActiveRecord::Base has_many :customers, :companies, :vendors end class Customer < ActiveRecord::Base belongs_to :user end class Company < ActiveRecord::Base belongs_to :user end class Vendor < ActiveRecord::Base belongs_to :user end 的信息(除非你真的关心你,否则没有理由性能)你可以像这样链接你的模型:

has_many

在这种情况下,由于用户与3种用户类型中的2种没有关联,因此您将使用与{0}关联案例很好地协作的{{1}}关联。您只需在代码中进行一些检查,以确保不会加倍。

相关问题