When to use self in Model?

前端 未结 4 938
天涯浪人
天涯浪人 2020-12-12 19:53

Question: when do I need to use self in my models in Rails?

I have a set method in one of my models.

class SomeData < ActiveRecord::B         


        
4条回答
  •  庸人自扰
    2020-12-12 20:14

    It's to make sure you're using the setter method and not scoping a new variable. It's a Ruby and AR usage detail that often trips people up (the other being the (mis-)use of an instance variable).

    Note there's already update_attributes! although I understand the desire to abstract.

    There's also toggle!, which might be even nicer, depending on your interface to the flag.

提交回复
热议问题