What is a good way to assign a model object to a symbol after saving it?

How to return the object after saving the model, for example:

      Message.new(:receiver => receiver, :sender => self, 
      :subject => subject,
      :body => body).save

      

I understand what I could do

      Message.last

      

But will there be any impact over a long period of traffic when the database is constantly being accessed? I am afraid that Message.last will return another record.

+2


source to share


1 answer


If you are directly creating an object, you can use the create method, which creates and returns it.

message = Message.create(...)

      



He does exactly what DR suggested. But only in one line of code :)

+1


source







All Articles