Should I use class method or instance method, and why?

后端 未结 4 639
生来不讨喜
生来不讨喜 2021-02-02 03:06

In my Rails app, when creating a business I have a form that has the following field:

   <%= check_box_tag(:default_company) %> 
   <%= label_tag(:defau         


        
4条回答
  •  情书的邮戳
    2021-02-02 03:58

    A few things: do you have a separate table for DefaultCompany? This seems like it should be a boolean flag on the company table.

    Next, is there an association between companies and users? If so, it seems the best way to do it would be

    In the user model

    def set_default_company(company)
      self.companies.each do |c|
        c.update_attributes(:default => false)
      end
      company.update_attributes(:default => true)
    end
    

    Or in the Company model

    def set_as_default
      update_attributes(:default_company => true)
    end
    

提交回复
热议问题