使用嵌套属性填写继承的 mongoid 文档
给定以下模型:
class Company
include Mongoid::Document
has_many :workers, autosave: true
accepts_nested_attributes_for :workers
attr_accessible :workers_attributes
end
class Worker
include Mongoid::Document
field :hours
attr_accessible :hours
belongs_to :company
end
class Manager < Worker
field :order
has_many :contributors, :class_name => "Worker"
attr_accessible :order, :contributors
end
class Contributor < Worker
field :task
belongs_to :manager, :class_name => "Worker"
attr_accessible :task
end
如何在控制器中创建公司中的经理并使用嵌套属性查看?
这是我的猜测:
def new
@company = Company.new
@company.workers = [Manager.new]
end
def create
@company = Company.new params[:user]
if @company.save
redirect_to root_url, :notice => "Company with manager created."
else
render :new
end
end
= semantic_form_for @company do |f|
= f.semantic_fields_for :workers do |worker_fields|
= worker_fields.inputs do
= worker_fields.input :hours
= worker_fields.input :order
问题是专门属于经理的订单字段在创建后不会持续存在。另外,当数据填充不正确时,会出现错误:
undefined method `order' for #<Worker:0x0000000646f018> (ActionView::Template::Error)
So is not a way fornested attribute to handleheimer in the models from mongoid?
该问题与 嵌套属性是否可以与继承结合使用有关? 除非使用 mongoid 代替活动记录。
老实说,这是我的代码的释义......真正的代码是更复杂的情况,尽管我相信这些都是相关的部分。如果您还有更多问题请询问。
更新:
我将视图更改为以下内容:
= semantic_form_for @company do |f|
- @company.workers.each do |worker|
- if worker._type == "Manager"
= f.semantic_fields_for :workers, worker do |worker_fields|
= worker_fields.inputs do
= worker_fields.input :hours
= worker_fields.input :order
我不再收到错误,但是嵌套属性不会正确更新公司对象。参数如下:
{"company"=> {"workers_attributes"=>{"0"=>{"hours"=>"30", "order" => "fish", "id"=>"4e8aa6851d41c87a63000060"}}}}
为简洁起见再次编辑。所以关键部分是“0”=>之间有一个哈希值。 {经理的数据}。工人数据似乎保存在哈希中。我希望数据看起来更像下面这样:
params = { company => {
workers_attributes => [
{ hours => "30", "order" => "fish" }
]}}
这是不同的,因为工作人员数据保存在数组而不是哈希中。是否还有另一个步骤可以正确保存嵌套属性?
谢谢
Given the following models:
class Company
include Mongoid::Document
has_many :workers, autosave: true
accepts_nested_attributes_for :workers
attr_accessible :workers_attributes
end
class Worker
include Mongoid::Document
field :hours
attr_accessible :hours
belongs_to :company
end
class Manager < Worker
field :order
has_many :contributors, :class_name => "Worker"
attr_accessible :order, :contributors
end
class Contributor < Worker
field :task
belongs_to :manager, :class_name => "Worker"
attr_accessible :task
end
How does one create a manager in a company in the controller and view using nested attributes?
Here's my guess:
def new
@company = Company.new
@company.workers = [Manager.new]
end
def create
@company = Company.new params[:user]
if @company.save
redirect_to root_url, :notice => "Company with manager created."
else
render :new
end
end
= semantic_form_for @company do |f|
= f.semantic_fields_for :workers do |worker_fields|
= worker_fields.inputs do
= worker_fields.input :hours
= worker_fields.input :order
problem is the order field which specifically belongs to the manager is not persisting after the create. Also when the data is improperly filled there is an error:
undefined method `order' for #<Worker:0x0000000646f018> (ActionView::Template::Error)
So is there a way for nested attributes to handle inheritance in the models from mongoid?
The question is related to Can nested attributes be used in combination with inheritance? except instead of active record using mongoid.
Honestly, this is a paraphrasing of my code... the real code is more complex situation although i believe these are all of the relevant parts. If you have more questions ask.
UPDATE:
I changed the view to the following:
= semantic_form_for @company do |f|
- @company.workers.each do |worker|
- if worker._type == "Manager"
= f.semantic_fields_for :workers, worker do |worker_fields|
= worker_fields.inputs do
= worker_fields.input :hours
= worker_fields.input :order
I do not get the error anymore, however the nested attributes do not update the company object properly. The params are the following:
{"company"=> {"workers_attributes"=>{"0"=>{"hours"=>"30", "order" => "fish", "id"=>"4e8aa6851d41c87a63000060"}}}}
Again edited for brevity. So the key part is that there is a hash between "0" => {data for manager}. The workers data seems to be held in a hash. I would expect the data to look more like the following:
params = { company => {
workers_attributes => [
{ hours => "30", "order" => "fish" }
]}}
This is different because the workers data is held in an array instead of a hash. Is there another step to get the nested attributes to save properly?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用什么版本的 Mongoid?因为我认为不鼓励使用refereneces_many——这与您的问题无关,只是想探究您正在使用的版本。在华丽的 Mongoid.org 上的文档中,得到这个,我必须以艰难的方式学习它,他们说要更新你的记录,你需要将自动保存设置为 true。这不准确。您甚至需要它来创建
这样的内容:
添加:
我正在重新阅读您的代码,我发现以下可能是问题:您的公司模型设置为 has_many :workers 并设置为在发生更改时接受 Worker 的嵌套属性, 正确的? Manager 模型中有一个名为 Order 的字段,它是 Worker 的子类。然而,您有一个表单,其嵌套字段部分指向 Worker 而不是 Manager,即实际具有 Order 字段的模型。这显然还不够,因为 Company 还没有having_many :managers,您可能还需要在 Company 模型中将其设置为 has_many :managers 。
what version of Mongoid are you using? Because I don't think the use of refereneces_many is encouraged -- Not that that's related to your problem here, just wanted to probe what version you're using. In the doc on the gorgeous Mongoid.org, get this, I had to learn it the hard way, they say for Updating your records, you need to the autossave set to true. That's NOT accurate. You need it for even creating
so:
ADDED:
I was re-reading your code, I spotted the following that might be the problem: Your Company model is set to has_many :workers and is set to accept nested attribbutes for Worker when changes come in, correct? And there is a field named Order in your Manager model which is subclassed from Worker. Yet you're having a form whose nested fields part is pointed at Worker not at Manager, the model that actually has the Order field. And that's obviously not enough, because Company isn't having_many :managers yet, you may need to set it to has_many :managers in the Company model as well.