In my Rails application, when creating a business, I have a form that has the following field:
<%= check_box_tag(:default_company) %> <%= label_tag(:default_company, "Set Company as Default") %>
Essentially, when I create a business, if they check this box, I need it to run something like the following code:
def set_default_company(company, user) exists = DefaultCompany.find(user.id) if exists exists.update_attributes(company: company) else DefaultCompany.create(company: company, user: user) end end
During training, I usually did this in my controller, but I try to follow best practices and use a thick model, a skinny controller, so I want to use this logic:
def create @company = Company.new(params[:company]) if @company.save if params[:default_company] Company.set_default_company(@company.id, current_user.id,) end flash[:notice] = "Company was successfully created." redirect_to @company else redirect_to new_company_path end end
That's where I got confused about whether to use a class method or an instance method to call set_default_company
. They both seem to work, and I see no benefit to either.
In addition to giving me any information about which method to use, if someone can show me a brief implementation of writing this method as a class or instance method, it can give me a better idea of โโwhy.
Here is how I wrote them:
def self.set_default_company(company, user)
Having written them in this way, I also see no advantage.