问题描述
我有一个系统,需要从主页上的一种登录表单登录三种用户类型:客户,公司和供应商.
I have a system where I need to login three user types: customers, companies, and vendors from one login form on the home page.
我在 http://github.com/binarylogic中创建了一个根据AuthLogic的示例应用程序工作的用户表. /authlogic_example .我添加了一个名为用户类型"的字段,该字段当前包含客户",公司"或供应商".
I have created one User table that works according to AuthLogic's example app at http://github.com/binarylogic/authlogic_example. I have added a field called "User Type" that currently contains either 'Customer', 'Company', or 'Vendor'.
注意:每种用户类型都包含许多不同的字段,因此我不确定单表继承是否是最好的方法(如果此结论无效,欢迎进行更正).
Note: each user type contains many disparate fields so I'm not sure if Single Table Inheritance is the best way to go (would welcome corrections if this conclusion is invalid).
这是一种多态关联,其中三种类型中的每一种都用用户记录标记"了吗?我的模型应该如何显示,这样我的用户表和用户类型客户,公司,供应商之间具有正确的关系?
Is this a polymorphic association where each of the three types is 'tagged' with a User record? How should my models look so I have the right relationships between my User table and my user types Customer, Company, Vendor?
非常感谢!
------更新------
------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
这是设置它们的有效方法吗?
Is this a valid way to set them up?
推荐答案
这听起来像是您正在尝试做的事情:
This is what it sounds like you are trying to do:
您有users
,可以在三个groups
之一中.如果那还不太正确,那么进行一些澄清将有所帮助.
You have users
that can be in one of three groups
. If that isn't quite right a little clarification would help.
由于AuthLogic只关心它的特殊字段,因此在用户模型中制作和使用其他字段并不繁琐.
Since AuthLogic only cares about it's special fields, making and using other fields in your user model is no biggy.
每个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
...
我不确定单表继承"是什么意思,但是如果您想将有关vendor
的信息保留在与用户表分开的表中(除非您真的非常担心性能,否则没有理由)您可以像这样链接您的模型:
I'm not sure what you mean by "single table inheritance" but if you want to keep information about a vendor
in a table separate from the users table (really no reason to unless you are REALLY concerned about performance) you can just link up your models like this:
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关联的情况下效果很好.您只需要在代码中进行一些检查,以确保不会加倍.
In this case, since a user would have no associations to 2 of the 3 user types, you are pushed into using the has_many
association which works nicely with the 0 association case. You would just have to do some checks in your code to make sure you don't double up.
这篇关于Rails:多态用户表AuthLogic是一个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!