2013-04-14 47 views
2

我有這兩款車型銷燬嵌入式Mongoid文件:通過指定「ID」(而不是發現它通過其母公司)

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/I show.html.haml有幾行代碼:

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

我已經嘗試了許多APPR在銷燬控制器動作中發生了一些浮動,但它們都指向了一個顯而易見的事實,即我試圖銷燬一個嵌入式文檔而不通過其父文件。但是現在我在我的視圖文件中通過它的id有了presentation_row,看起來我不應該被允許銷燬它。

一個空的破壞作用的錯誤消息,供參考:

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 

我應該投入銷燬行動?

回答

7

這是mongoid的限制。 Quoth貢獻者「嵌入式文檔必須始終通過父級訪問」。 https://github.com/mongoid/mongoid/issues/348

請注意,這不是mongodb的限制。解決方法得到PresentationRow對象從它的ID是...

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,如果你有很多。

+1

謝謝。那就是訣竅。我會將答案標記爲已接受,但您是否可以首先使用以下更正進行編輯:1)在第1行的末尾添加_.first_,否則將返回一組演示文稿而不是單個演示文稿。 2)在_pr.id_之後添加_.to_s_,否則返回nil。 – ChristofferJoergensen

+0

已更新。感謝您的更正。 – Leopd

+0

使用更新的MongoDB版本,您可以爲嵌入字段建立索引,並且應該很容易找到半直接嵌套的記錄(您仍然需要獲取父級) –