我应该使用类方法还是实例方法,为什么? [英] Should I use class method or instance method, and why?

查看:74
本文介绍了我应该使用类方法还是实例方法,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails应用程序中,创建公司时,我具有一个包含以下字段的表单:

In my Rails app, 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

在学习时,我通常会在控制器中做这些事情,但是我试图遵循最佳实践并使用胖模型,瘦控制器,所以我想使用这样的逻辑:

While learning, I would usually do that stuff in my controller but i'm trying to follow best practices and use a fat model, skinny controller, so I'm wanting to use logic like this:

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

在这里,我对于是否使用类方法或实例方法来调用set_default_company感到困惑.他们俩似乎都可以工作,但我看不到彼此的好处.

Here is where I am getting confused on whether to use a class method or an instance method, to call set_default_company. They both seem like they would work and I can't see a benefit to one or the other.

除了向我提供有关使用哪种方法的任何信息外,如果有人可以向我展示将其作为类方法与实例方法进行编写的简短实现,则可能会使我对原因有所了解.

In addition to giving me any information as to which method to use, if someone could show me a brief implementation of writing that as a class method vs. instance method it may give me a better understanding as to why.

这是我写它们的方式:

def self.set_default_company(company, user)
  # Logic here
end

def set_default_company(company, user)
  # Logic here
end

以这种方式书写它们,我也看不到任何好处.

Writing them that way I don't see a benefit to either.

推荐答案

顾名思义,模型上的实例方法应用于与用户的特定实例相关的逻辑/操作(在该实例上使用该方法)因此,您可以考虑将用户的默认公司设置为User上的实例方法.类方法用于无法在模型的单个实例上运行的事物,或者用于您没有可用的实例的情况.例如您可能有一个用于整理数据库的类方法,例如User.purge_expired_users,它不适用于单个用户对象.

As their name suggests, instance methods on a model should be used for logic/operations that relate to a specific instance of a user (the one on which the method is called.) So you could think of setting the default company for a user as an instance method on User. Class methods are for things which don't operate on an individual instance of a model or for cases where you don't have the instance available to you. e.g. you might have a class method to tidy up your database such as User.purge_expired_users which would not apply to an individual user object.

例如

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

然后您的控制器方法将如下所示:

then your controller method would look like:

def create
  @company = Company.new(params[:company])

  if @company.save
    if params[:default_company]
      current_user.set_default_company @company
    end
    flash[:notice] = "Company was successfully created."
    redirect_to @company
  else
    redirect_to new_company_path
  end
end

或者,您可以从另一个角度考虑这种关系,并在Company上放置一个实例方法,例如company.set_as_default_for(user).

Alternatively, you could think of the relationship from the other perspective and put an instance method on Company e.g. company.set_as_default_for(user).

这篇关于我应该使用类方法还是实例方法,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆