English 中文(简体)
铁路:通过相关模型对一个模型进行多种“类型”?
原标题:Rails: Multiple "types" of one model through related models?

我的附录中有一个<代码>User模型,我谨储存基本的用户信息,如电子邮件地址、头和最后名称、电话号码等。

我的系统中也有许多不同的用户,包括销售代理人、客户、客人等。

我谨使用同样的<代码>。 用户模型作为所有其他模型的基础,因此我不必将所有相关作用的所有领域纳入一个模式,并视需要(缩小数据库重复领域的范围,以及从一个类型用户向另一个用户提供易于流动)。

因此,我喜欢的是:

User
-- first name
-- last name
-- email
--> is a "client", so
---- client field 1
---- client field 2
---- client field 3

User
-- first name
-- last name
-- email
--> is a "sales agent", so
---- sales agent field 1
---- sales agent field 2
---- sales agent field 3

and so on...

此外,在出现新的用户迹象时,我希望新用户自动被指定为“客户”的作用(这里谈论数据库领域并不授权,尽管我希望最终将这一逻辑纳入我的用户授权)。 我有一个多步的信号,试图用wizardly建造。 第一步是轻松的,因为Im只是指基数<编码>User 模型(例如first_name所包含的领域,而第二步则比较简单,因为它应当从相关模型(如以上模型<代码>>>的示例)的田地(client_field_1 <>/code>或>)中点域名/client_field_2,因为这些领域是的一部分。 用户

这是否有意义? 让我知道,这是否完全清楚,我试图以不同的方式解释。

谁能帮助我这样做? 我如何这样做?

最佳回答

它认为,这里有两种合理的做法,但将取决于你的要求的细微差别。

你可以使用单一表格(STI)来做你想要做的事情,因为用户只是其他名称为销售代理人或客户的基类。 这些次类中的每一类人都可确定自己的鉴定。 你们都需要做这项工作,这是一个称为“类型”的细列一栏,而“积极记录”则将做其他工作:

class User < ActiveRecord::Base
end

class Agent < User
end

另一种选择是,在你储存各种相关数据的地方,拥有一些自由格式的领域,而且只是在操作时间对这些数据进行不同的解释。 您可制定这样的结构:

class User < ActiveRecord::Base
  has_one :agent_role,
    :dependent => :destroy
end

class AgentRole < ActiveRecord::Base
  belongs_to :user

   # Represents the agent-specific role fields
end

这样做的好处是允许发挥多重作用,如果你利用这种作用,那么这种作用就具有多重作用。

问题回答

如塔德曼所建议的,如果你正在使用主动记录(从铁路3起,很容易改变口服体液处理法),科学、技术和革新很可能符合你的要求。 基本信息可在AR 文件网页上查阅,但这里有一些额外信息。

  • Define one model per file. Otherwise there are some initialization troubles. Assuming Client inherits from User all in a single file, Client objects cannot be created as long as a User constructor has not been called once. One file per model circumvents the problem.
  • All attributes through the hierarchy are defined one-shot in the top class. This is for performance issues, but it seems disturbing many people in blog posts. In short, the Ruby code is object-oriented and encapsulates properly the attributes. The DB contains everything in a single table, with the extra "type" column to distinguish where they belong in the hierarchy. This is only a "trick" to represent inheritance trees in relational databases. We must be aware that the ORM mapping is not straightforward. The image on this site from Martin Fowler may help understand the situation.

此外,您希望任何新的用户成为客户,客户继承用户。 为此,你可以简单地将任何新用户当成客户。 在你控制下,创建用户:

user = Client.new
# Do something to user
user.save
#=> <Client id: 1, name: "Michael Bolton", email: "mike@bolton.net", created_at: "2010-05-30 03:27:39", updated_at: "2010-05-30 03:27:39">

以上所有措施在使用主动协议时仍然适用铁路3。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...