I have a Post
Class and a Comment
Class. I have a post object and want to convert it to a Comment object. I went through Rails api and found becomes(klass).For now there is not association between a Post and Comment. So i tried
@post.becomes(Comment)
but becomes method could not be found for @post
object. Am i missing something ?
Put the methods you want both models to use on a module. Then include that module in both models.
Please note that becomes returns a new object but does not modify the existing object. You need to do the assignment yourself
@post = @post.becomes(Comment)
See the documentation ActiveRecord::Persistence#becomes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With