Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在继承模型的顶部添加其他字段,并公开所有超级和子类字段_Ruby On Rails_Inheritance_Database Design_Associations_Single Table Inheritance - Fatal编程技术网

Ruby on rails 在继承模型的顶部添加其他字段,并公开所有超级和子类字段

Ruby on rails 在继承模型的顶部添加其他字段,并公开所有超级和子类字段,ruby-on-rails,inheritance,database-design,associations,single-table-inheritance,Ruby On Rails,Inheritance,Database Design,Associations,Single Table Inheritance,我的目标: 我正在尝试创建两种不同类型的用户,使用不同的配置文件 Barber、Client、BarberProfile和ClientProfile 我有一个基本的用户对象,其中包含电子邮件、密码,以及design跟踪的所有其他字段 我希望基本用户模型有一个配置文件,它跟踪我希望所有用户都拥有的所有基本信息。例如,姓,姓,化身,等等 我使用单表继承来创建两种不同类型的用户:Client和Barber 我希望每种类型的用户都有一个与之关联的基本配置文件,然后分别有属于BarberProfile和C

我的目标:

我正在尝试创建两种不同类型的用户,使用不同的配置文件

Barber
Client
BarberProfile
ClientProfile

我有一个基本的
用户
对象,其中包含
电子邮件
密码
,以及design跟踪的所有其他字段

我希望基本
用户
模型有一个
配置文件
,它跟踪我希望所有用户都拥有的所有基本信息。例如,
化身
,等等

我使用单表继承来创建两种不同类型的用户:
Client
Barber

我希望每种类型的用户都有一个与之关联的基本
配置文件
,然后分别有属于
BarberProfile
ClientProfile
的附加字段

BarberProfile
将拥有
Barber
需要的东西,但
客户端却没有。例如,a
bio
ClientProfile
将拥有
客户所需的东西,但
理发师却没有。例如,
hair\u type

我目前拥有的以及我的问题:

如上所述,我为
User
Profile
创建了一个表。所以我可以调用
user.profile.first\u name
。为了添加额外的字段,我创建了一个
BarberProfile
ClientProfile

如果用户类型为
Barber
,我只希望能够引用
user.profile.bio
。但是
bio
不是基本配置文件的一部分。因此,在这种情况下,我必须创建一个关联的
Profile
和关联的
BarberProfile
,以获得所需的一切。我可以只做
user.profile.first\u name
user.barber\u profile.bio
,但感觉很凌乱,我从本质上相同类型的模型中创建了两种不同的关联。我觉得让
BarberProfile
继承
Profile
中的所有字段并在顶部添加自己特定的
BarberProfile
字段应该很简单

如何在Rails中实现这一点


编辑:我想这样做的主要原因之一是,我可以在同一表单中为
理发师更新
名字
生物
。同样地,对于
客户机
名字
头发类型
,,如果您想避免在用户上使用Profile和Client/BarberProfile的两个关联,我想您应该让ClientProfile和BarberProfile扩展Profile(单表继承)以及它们中的每一个“有一个:barber\u profile\u data”(我不确定如何调用它)。要修复长方法调用,可以使用委托方法

class Barber > User
  has_one :barber_profile
  delegate :bio, to: :barber_profile

class Client < User
  has_one :client_profile
  delegate :first_name, to: :client_profile

class BarberProfile < Profile
  has_one :barber_profile_data
  delegate :bio, to: :barber_profile_data

class ClientProfile < Profile
  has_one :client_profile_data
  delegate :first_name, to: :client_profile_data
class Barber>User
有一个:理发师简介
代表:bio,收件人::barber_个人资料
类客户端<用户
有一个:客户档案
委托人:第一个姓名,收件人::客户端配置文件
类配置文件<配置文件
有一个:理发师档案数据
代理人:bio,收件人::barber\u个人资料\u数据
类ClientProfile

然后,当您执行“@barber.bio”时,它应该在内部调用“@barber.barber\u profile.barber\u profile\u data.bio”。

这听起来是一个很好的使用案例。在MTI中,您使用附加表来装饰基本模型

MTI的主要优点是
客户机
理发师
表可以包含该类型与STI的特定列,这需要您根据设计将所有内容塞进
用户
(这就是为什么他们称之为单表)

这是gem的一个例子

class用户
请注意,
Barber
Client
不应该是真正的子类。
ActiveRecord::ActsAs
而是委托给“actable”类


然后,您可以执行
Barber.all
Client.all
来获取特定类型或使用
User.all.map(:specific)
获得各种类型的装饰用户。

感谢您的输入。我喜欢这个想法,因为扩展配置文件是我的第一本能,而且看起来非常直观。因此,在您的建议中,barber_profile_数据作为一个单独的表存在?是否可以使用此设置从一个表单更新bio?是的,BarberProfileData应该有自己的表。关于更新,这应该是可能的,检查rails form_中的方法字段并使用它。无论如何,您可以随时自定义更新操作,并在需要设置正确字段时进行操作。
create_table "barbers", force: :cascade do |t|
  # only the specific attributes
  t.text     "bio"
end

create_table "clients", force: :cascade do |t|
  # only the specific attributes
  t.string   "hair_type"
end

create_table "users", force: :cascade do |t|
  t.string   "email"
  t.string   "first_name"
  t.string   "last_name"
  # ... all the other common attributes
  t.integer  "actable_id"
  t.string   "actable_type"
  t.datetime "created_at",   null: false
  t.datetime "updated_at",   null: false
end
class User < ApplicationRecord
  actable
end

class Barber < ApplicationRecord
  acts_as :user
end

class Client < ApplicationRecord
  acts_as :user
end