简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM