2013-02-26 62 views
5

我使用mongoid與色器件invitable,mongoid as_document錯誤

用戶分配角色後,我發現「爲陣**未定義的方法`as_document」 **」下面的錯誤

,有什麼建議?

invitable = find_or_initialize_with_error_by(:email, attributes[:email]) 
invitable.attributes = attributes 
# scope_id attribute does not set properly 
invitable.roles.map {|r| r.scope_id = attributes[:roles_attributes]["0"][:scope_id]} 

if invitable.persisted? && !invitable.invited? 
    invitable.errors.add(:email, :taken) 
elsif invitable.email.present? && invitable.email.match(Devise.email_regexp) 
    invitable.invite! 
end 

我在做什麼錯?

+0

面對同樣的錯誤。任何人都可以幫忙嗎? – 2013-02-27 06:14:56

+1

你發佈的代碼bud沒有告訴我們什麼行會拋出錯誤。你可以發佈真正的堆棧跟蹤或一些更多的信息? – alestanis 2013-02-27 11:04:05

回答

1

這很可能是因爲as_document不能針對數組,僅對單個對象起作用。

+0

您的問題中沒有足夠的代碼來提供更多詳細信息或可能的更正版本。 – 2013-05-19 00:33:22

+0

我認爲這是正確的答案。嘗試使用「whatever.first.as_document」,無論您嘗試表示什麼對象。 – jschorr 2013-08-19 16:51:13

0

這是一個Mongoid和has_many關係的錯誤。

必須爲has_many關係定義方法'as_document',就像爲embeds_many關係定義一樣。

我準備做一個pull request有這個問題固定的,在此期間,你可以在你的Gemfile定義mongoid像這樣:

gem 'mongoid', :git => https://github.com/mrjlynch.git 
0

這發生在我身上有MongoId 5.1.0時,在一方向我有「embeds_many」,並在另一個方向我有「belongs_to」。

據我所知,embeds_many的反向應爲embedded_in。 將反向關係更改爲embedded_in解決了我的問題。我不得不承認,這是一個非常模糊的錯誤信息。

相關問題