gpt4 book ai didi

ruby-on-rails - 通过指定其 "id"(而不是通过其父级查找)来销毁嵌入的 Mongoid 文档

转载 作者:行者123 更新时间:2023-12-04 07:38:54 24 4
gpt4 key购买 nike

我有这两个模型:

class Presentation
include Mongoid::Document
embeds_many :presentation_rows
end

class PresentationRow
include Mongoid::Document
embedded_in :presentation
end

在我的 presentation_rows_controller.rb 中,我有这些代码行:

def show
@presentation = Presentation.find(params[:id])
@presentation_rows = @presentation.presentation_rows
end

def destroy
...
...
end

在我的 presentation_rows/show.html.haml 中,我有这些代码行:

- @presentation_rows.each do |presentation_row|
= link_to "Delete", presentation_row, method: :delete

我在销毁 Controller 操作中尝试了很多方法,但它们都指向一个明显的事实,即我试图在不通过其父级的情况下销毁嵌入的文档。但是现在我在我的 View 文件中有 presentation_row 的 id,我不应该被允许销毁它似乎很愚蠢。

带有空销毁操作的错误消息,仅供引用:

Started DELETE "/en/presentation_rows/516af0a983c336708300000f" for 127.0.0.1 at 2013-04-14 20:08:47 +0200
Processing by PresentationRowsController#destroy as HTML
Parameters: {"authenticity_token"=>"KHGG2dsTseCl88okOKW9JAlHb+VaK2lKIxb0ptAIC7A=", "locale"=>"en", "id"=>"516af0a983c336708300000f"}
MOPED: 127.0.0.1:27017 QUERY database=shop_import_development collection=users selector={"$query"=>{"_id"=>"511a813a83c336a0ea000001"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 fields=nil (0.3152ms)
MOPED: 127.0.0.1:27017 QUERY database=shop_import_development collection=presentations selector={"_id"=>"516af0a983c336708300000f"} flags=[:slave_ok] limit=0 skip=0 fields=nil (0.2129ms)
Completed 500 Internal Server Error in 2ms

Mongoid::Errors::DocumentNotFound (
Problem:
Document(s) not found for class Presentation with id(s) 516af0a983c336708300000f.
Summary:
When calling Presentation.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): 516af0a983c336708300000f ... (1 total) and the following ids were not found: 516af0a983c336708300000f.
Resolution:
Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.):
app/controllers/presentation_rows_controller.rb:16:in `correct_user?'


Rendered /Users/christoffer/.rvm/gems/ruby-1.9.3-p385/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered /Users/christoffer/.rvm/gems/ruby-1.9.3-p385/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /Users/christoffer/.rvm/gems/ruby-1.9.3-p385/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (9.2ms

我应该在 destroy Action 中加入什么?

最佳答案

这是 mongoid 的一个限制。贡献者说:“必须始终通过父级访问嵌入式文档。” https://github.com/mongoid/mongoid/issues/348

请注意,这不是 mongodb 的限制。从其 id 获取 PresentationRow 对象的解决方法是...

pres = Presentation.where('presentation_rows._id' => Moped::BSON::ObjectId(params[:id])).first
row = pres.presentation_rows.detect { |pr| pr.id.to_s == params[:id] }
row.destroy

如果索引很多,请将索引添加到 presentation_rows._id

关于ruby-on-rails - 通过指定其 "id"(而不是通过其父级查找)来销毁嵌入的 Mongoid 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16002994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com