How to use becomes in Rails

I have class PostClass and Comment. I have a post object and you want to convert it to a Comment object. I went through the Rails api and found that it was becoming (klass). So far, there is no connection between Post and Comment. So I tried

@post.becomes(Comment)

but method @postnot found. Did I miss something?

+3
source share
3 answers

I assume that you are talking about classes originating from ActiveRecord :: Base. In this case, I think you have two options if you really want to convert messages to comments (which seems a little strange to me):

  • (, ) , ,
  • , post, post.
0

, . .

+3

Note that a new object will be returned, but will not modify an existing object. You need to complete the task yourself

@post = @post.becomes(Comment)

See documentation ActiveRecord :: Persistence # will become

+3
source

Source: https://habr.com/ru/post/1739418/


All Articles