Mongoid as_document error

I use a mangoid with an invention,

after assigning roles to the user, I found the following error

"** undefined method` as_document 'for Array **", any suggestions?

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

What am I not doing?

+5
source share
3 answers

This is most likely because it as_documentdoes not work against an array, only individual objects.

+1
source

This is a bug with the Mongoid and has_many relationships.

The 'as_document' method must be defined for has_many relationships, as defined for embeds_many relationships.

I am going to make a pull request to fix this problem, in the meantime you can define mongoid in your gem file like this:

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

It happened to me with MongoId 5.1.0, when in the same direction I was " embeds_many ", and "in a different direction prinadlezhit_to ".

From what I know, the flip side of embeds_many should be embedded_in . Changing feedback with embedded_in fixed this problem for me.

I must admit, this is a very obscure error message.

0
source

All Articles