Rails:Polymorphic User Table与AuthLogic一个好主意?
我有一个系统,我需要从主页上的一个登录表单登录三种用户类型:客户,公司和供应商。
我创建了一个User表,该表根据AuthLogic的示例应用程序工作, url为http://github.com/binarylogic/authlogic_example 。 我添加了一个名为“用户类型”的字段,该字段当前包含“客户”,“公司”或“供应商”。
注意:每个用户类型包含许多不同的字段,因此我不确定单表inheritance是否是最好的方法(如果此结论无效,则欢迎更正)。
这是一个多态关联,其中三种类型中的每一种都用用户记录“标记”了吗? 我的模型应该如何显示,以便我的用户表和我的用户类型客户,公司,供应商之间有正确的关系?
非常感谢!
—— UPDATE ——
目前的设置:
#User model class User true end #Customer model (Company and Vendor models are similar) class Customer :authenticable acts_as_authentic end
这是设置它们的有效方法吗?
这就是你想要做的事情:
您的users
可以属于三个groups
中的一个。 如果这不是很正确,那么一点澄清会有所帮助。
由于AuthLogic只关心它的特殊字段,因此在用户模型中制作和使用其他字段并不重要。
每个user
可能都是这样的:
#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 ...
我不确定你的“单表inheritance”是什么意思,但是如果你想把一个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
在这种情况下,由于用户与3种用户类型中的2种没有关联,因此您将使用has_many
关联,该关联与0关联案例很好地协作。 您只需在代码中进行一些检查,以确保不会加倍。