ruby-on-railsmongoidruby-on-rails-4.2mongoid5

Mongoid Rails update error for documents having embedded documents


I have a model called User. User embeds_many posts.

For User records having embedded posts, whenever I try to update any other field, I am getting the error

NoMethodError: undefined method `each' for false:FalseClass

I am using update as follows

user = User.find('56da7307421aa90ca4000000')
user.update(likes: 12)

If I remove embeds_many :posts from User model file, the above update query works fine.


Solution

  • Finally I figured out the issue myself. The issue was the result of bad written association. It has to be like given below. But I had missed the embedded_in relationship inside Post model.

    class User
      embeds_many :posts
    end
    
    class Post
      embedded_in :user
    end
    

    Writing embedded_in :user inside Post model solved the issue.