-
-
Save sonianand11/6e1b9be64e0e502e3216325d972d2281 to your computer and use it in GitHub Desktop.
how to use build method to create new object
This file contains hidden or 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 < ActiveRecord::Base | |
has_many :comments | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :post | |
end | |
post = Post.first | |
comment = Comment.new | |
comment.post_id = post.id | |
puts comment.post_id # prints 1 | |
comment = post.comments.build | |
puts comment.post_id # prints 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment