Thursday, April 21, 2011

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

How do I return the object after a model save like:

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

I understand I could probably do a

      Message.last

But will there be any implications during a high traffic time period where the database is constantly being accessed? I'm afraid that Message.last will return another record.

From stackoverflow
  • If you're directly creating the object, you can use the create method, which creates and returns it.

    message = Message.create(...)
    

    It does the same then what's suggested by DR. But in only one code line :)

0 comments:

Post a Comment