Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Post
- include Mongoid::Document
- field :title, :type => String
- has_many :comments do
- def update(selector, params)
- @target.each { |t| t.update_attributes(params) if t.matches?(selector) }
- end
- end
- end
- class Comment
- include Mongoid::Document
- field :body, :type => String
- belongs_to :post, :inverse_of => :comments
- end
- @post = Post.create(:title => 'Friday post')
- @post.comments.create(:body => 'yay its friday')
- @post.comments.create(:body => 'woohoo for tuesday!')
- comments = @post.comments.where(:body => 'woohoo for tuesday')
- comments.each { |comment| comment.body = "New Value" }
- comments.first.body = "New Value"
- @post.comments.update({:_id => params[:id]}, params[:comment])
Add Comment
Please, Sign In to add comment