[英]How to make work single table inheritance and attr_accessible?
In a rails application, I have a base model like this : 在rails应用程序中,我有一个这样的基本模型:
class User < ActiveRecord::Base
attr_accessible :email, :name
end
And an inheritance from this model, like this : 和这个模型的继承,像这样:
class Provider < User
belongs_to :user
attr_accessible :business_name, :contact_name, :business_phone_number,
:business_email, :website, :user_id
end
When I do : 当我做 :
Provider.new.user_id
It say NoMethodError: undefined method 'user_id' for #<Provider:0x007f63ec8b3790>
. 它说
NoMethodError: undefined method 'user_id' for #<Provider:0x007f63ec8b3790>
。
When I just do Provider.new
I have this: 当我刚做
Provider.new
我有这个:
#<Provider id: nil, name: nil, email: nil, created_at: nil, updated_at: nil, type: "Provider">
This is a part of my schema.rb : 这是我的schema.rb的一部分:
create_table "providers", :force => true do |t|
t.string "business_name", :null => false
t.string "contact_name", :null => false
t.string "business_phone_number", :null => false
t.string "business_email", :null => false
t.string "website"
t.integer "user_id"
end
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
As you can see, the attributes accessibles for Provider are not accessible. 如您所见,无法访问Provider的可访问属性。 Do you have a solution?
你有解决方案吗?
Thanks! 谢谢!
As discussed in the answer to Single Table Inheritance to refer to a child class with its own fields , ActiveRecord
's implementation of Single Table Inheritance does not involve additional tables. 正如单表继承的答案中所讨论的,它引用具有自己字段的子类 ,
ActiveRecord
的单表继承实现不涉及其他表。 All subclasses share the same table and rely on a type
field to identify subclasses. 所有子类共享同一个表,并依赖
type
字段来标识子类。 That explains why your Provider
instances only contain the fields available in your users
table, including the type
field that Rails adds for the subclassing support. 这就解释了为什么您的
Provider
实例只包含users
表中可用的字段,包括Rails为子类支持添加的type
字段。 Your providers
table is unused. 您的
providers
表未使用。
The same table is used at the database level. 在数据库级别使用相同的表。 STI is achieved by adding a new column to the existing table.
通过向现有表添加新列来实现STI。 The new column usually named 'type' is used to hold the information whether it is a 'Provider' or 'User'
通常名为“type”的新列用于保存信息,无论是“提供者”还是“用户”
Here is how the the model queries translate to a sql query 以下是模型查询转换为sql查询的方式
When the User model is queried, as per your example, it searches 'users' table and includes all available type values in the condition 查询用户模型时,根据您的示例,它会搜索“用户”表并包含条件中的所有可用类型值
> User.first
SELECT `users`.* FROM `users` WHERE `users`.`type` IN ('User', 'Provider') ORDER BY `users`.`id` ASC LIMIT 1
When the Provider model is queried, type is set as 'Provider' in the condition and so the results get filtered. 查询提供者模型时,在条件中将类型设置为“提供者”,因此结果将被过滤。 The underlying database table remains the same ie 'users'
底层数据库表保持不变即“用户”
> Provider.first
SELECT `users`.* FROM `users` WHERE `users`.`type` IN ('Provider') ORDER BY `users`.`id` ASC LIMIT 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.