Should a model method call 'save' itself? Should a model method call 'save' itself? ruby ruby

Should a model method call 'save' itself?


If your method really, really needs to do all that, so be it.

However, I would make it clear from looking at the method why you're doing that (comments might be good here), and would definitely make this a bang_method! so that it is clear to whoever invokes it that this method is liable to mess with the object as much as it likes.

Also, the method name result (which, I know, probably isn't your real method name) somewhat implies that you're just fetching data, and little more. Maybe load_result! would be more appropriate here, to make it clearer that you're not just accessing an attribute, but are, in fact, performing heavy operations to get it.


There are definitely times when it is necessary for a model to persist itself. But it's worth considering whether save is the best method for your application.

In a current example, we have a model that processes a file asynchronously in a long-running method (we are spinning the process off using sidekiq.) Inside the method, a persistent attribute is updated regularly so the status information is available to other requests.

We're using update_column rather than save, because

  1. We don't want or need the overhead of the AR callbacks, and we particularly want to skip validation to ensure the update occurs surely and immediately.
  2. We only need to update a single attribute. Using update_column avoids the need to determine whether any other attributes need to be saved (or not saved.)

Inside the model, methods like

  • update_column
  • save(:validate => false) (granted, same method, but different options)
  • touch

etc, may often be a more appropriate way to persist changes than a plain save.


When does a program save data on a file?

a) Only when user requires it (directly or indirectly)? -- this is controller case

b) Only when the program achieves part of its correctness and data integrity? -- this is model case

c) Both.

I would vote for (c). I hope this discrimination straightens things a bit.

Additionally, from a object-oriented design point of view, method save() belongs to the public contract of its class; it can be invoked by anyone. Given this, a class is responsible for its public contract and, if needed, an object can invoke its own methods at will.