Loading...

WellRailed@googlegroups.com

[Prev] Thread [Next]  |  [Prev] Date [Next]

Re: [WellRailed] A question of style Philip Murray Thu Feb 02 16:09:43 2012

On 25/01/2012, at 10:01 AM, Henry Maddocks wrote:

> Something that has bothered me for a while is should model methods that set 
> attributes call save or should the caller do it?
> 
> ie. This...
> 
> class Project < ActiveRecord::Base
>   def paid
>     self.state = PAID
>     self.save
>   end
> 
> end
> 
> or this...
> 
> my_project.paid
> my_project.save
> 

I sometimes provide both, and the method with a bang does the save for you


class Project < ActiveRecord::Base
        def paid!
                paid; save
        end

        def paid
                self.state = 'PAID'
        end
end

Then you can have the choice, if you're making other changes to your model 
before saving it

Cheers


Phil
        

-- 
You received this message because you are subscribed to the Google Groups 
"WellRailed" group.
To post to this group, send email to [EMAIL PROTECTED]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/wellrailed?hl=en.