I often write stuff that looks like this:
def AModel < ActiveRecord::Base
belongs_to :user
def SomeCodeThatDoesSomeCalculations
# some code here
end
def SomeCodeThatDoesSomeCalculations!
self.SomeCodeThatDoesSomeCalculations
self.save
end
end
Is there a better way to generate the functions with the suffix “!” ?
If you’re doing it really often you can do smth like that:
If you want that in multiple models may be you’d like to create your own base class for them containing this
define_with_saveclass method, or you can add it toActiveRecord::Baseitself if you are sure you need it.Btw, I hope you’re not really naming you your methods in
SomeCodeThatDoesSomeCalculationsnotation as they are usually named likesome_code_that_does_some_calculations.