A related style issue, for me: I'd tend to make method names active verbs:
class Project < ActiveRecord::Base
def pay
self.state = PAID
end
def pay!
pay
save
end
def paid?
self.state == PAID
end
end
In this specific example, I'd use Project.paid as a scope to retrieve all paid
projects.
On 25/01/2012, at 10:06 AM, Philip Murray wrote:
> 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.
--
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.