-
-
Save bobbywilson0/294166 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post | |
include Mongoid::Document | |
field :title, :type => String | |
has_many :comments | |
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!') | |
# PROBLEM: how do i update the body attribute on that second post I added? | |
# @post.comments.where(:body => 'woohoo for tuesday!') | |
# The above query returns a criteria object with the document (Post in this case) that matches, but how do i select and update the proper comment? | |
comment = @post.comments.select { |c| c.body == 'woohoo for tuesday!' } | |
comment.body = "jk, I f'ing hate tuesdays" | |
@post.save |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment