Guest User

Untitled

a guest
Apr 8th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. class Post
  2. include Mongoid::Document
  3. field :title, :type => String
  4. has_many :comments do
  5. def update(selector, params)
  6. @target.each { |t| t.update_attributes(params) if t.matches?(selector) }
  7. end
  8. end
  9. end
  10.  
  11. class Comment
  12. include Mongoid::Document
  13. field :body, :type => String
  14. belongs_to :post, :inverse_of => :comments
  15. end
  16.  
  17. @post = Post.create(:title => 'Friday post')
  18. @post.comments.create(:body => 'yay its friday')
  19. @post.comments.create(:body => 'woohoo for tuesday!')
  20.  
  21. comments = @post.comments.where(:body => 'woohoo for tuesday')
  22.  
  23. comments.each { |comment| comment.body = "New Value" }
  24. comments.first.body = "New Value"
  25.  
  26. @post.comments.update({:_id => params[:id]}, params[:comment])
Add Comment
Please, Sign In to add comment