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

后端 未结 4 550
生来不讨喜
生来不讨喜 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:41

    I would actually make set_default_company an instance method on User. A User has a default Company; why should a Company need to what users it is default for?

    class User
        def set_default_company(company)
            exists = DefaultCompany.find(id)
            if exists
                exists.update_attributes(company: company)
            else  
                DefaultCompany.create(company: company, user: self)
             end
         end
     end
    

提交回复
热议问题