Rails:助手和模型 - 在哪里组织代码

发布于 2024-08-30 17:48:11 字数 474 浏览 7 评论 0原文

我越来越多地将所有代码放入有关 MVC 的模型和帮助程序中。

然而,有时我不确定在哪里组织代码。它应该进入模型还是应该进入助手。各有什么好处。是一个更快还是它们相同。我听说过所有模型都会被缓存,所以看起来这将是放置我的大部分代码的更好地方。

例如,这是一个在模型或帮助程序中工作的场景:

 def status
  if self.purchased
   "Purchased"
  elsif self.confirmed
   "Confirmed"
  elsif self.reserved
   "Reserved"
  else
   "Pending"
  end

结束时

,我不需要像在数据库中那样保存此状态,因为有用于购买、确认和保留的布尔字段。那么为什么要把它放在模型中或者为什么要把它放在助手中呢?

因此,我不确定将代码放入模型或助手(如果可以同时放入两者中)的最佳实践或好处。

More and more I'm putting all of my code in models and helpers concerning MVC.

However, sometimes I'm not sure where to organize code. Should it go into the model or should it go into a helper. What are the benefits of each. Is one faster or are they the same. I've heard something about all models getting cached so it seems then like that would be a better place to put most of my code.

For example here is a scenario that works in a model or in helper:

 def status
  if self.purchased
   "Purchased"
  elsif self.confirmed
   "Confirmed"
  elsif self.reserved
   "Reserved"
  else
   "Pending"
  end

end

I don't need to save this status as in the database because there are boolean fields for purchased, and confirmed, and reserved. So why put this in a model or why put it into a helper?

So I'm not sure of the best practice or benefits gained on putting code into a model or into helper if it can be in both.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

护你周全 2024-09-06 17:48:11

您的具体示例包括一条业务规则,即如果模型的实例均已购买并确认,则正确的状态是“已购买”而不是“已确认”

所以在您的示例中,我肯定会将方法放入模型中因为它正在编码您的应用程序业务规则之一。

另一个示例:

def status_string
  case status
    when 0: "Purchased"
    when 1: "Confirmed"
    else
      "Pending"
   end
end

在这种情况下,可以在视图帮助程序或模型中合理地定义 status_string 方法 - 它与任何业务规则无关,它正在更改值的表示形式。我将其放入模型中,因为我倾向于只将与 html 相关的 sw 放入视图助手中。但根据您的国际化方案,类似的方法可能更好地放置在视图助手中。

View Helper 的一个很好的例子是一种应用程序范围的方法,用于将日期时间值转换为应用程序的标准表示形式。例如

# application_helper.rb
def date_long_s(d)
  d.strftime("%A, %b *%d, %Y *%I:%M %p")
end

Your specific example is including a business rule in the sense that if the instance of the model is both purchased and confirmed then the proper status is "purchased" not "confirmed"

So in your example, I'd definitely put the method in the model since it is coding one of your applications business rules.

A different example:

def status_string
  case status
    when 0: "Purchased"
    when 1: "Confirmed"
    else
      "Pending"
   end
end

In this case, the status_string method could be reasonably defined either in a View Helper or Model--it has nothing to do with any business rules, it is changing the representation of the value. I'd put it in the model since I tend to only put html-related sw into the View Helpers. But depending on your internationalization scheme, a similar method might be better placed in the View Helper.

A good example of a View Helper is an application-wide method to transform date time values into the standard representation for your app. Eg

# application_helper.rb
def date_long_s(d)
  d.strftime("%A, %b *%d, %Y *%I:%M %p")
end
瞳孔里扚悲伤 2024-09-06 17:48:11

这确实是主观的,我同意,有时不清楚某些东西是否属于模型或助手。

例如:

# using model
status ? status.nice_name : "Pending" 
# using helper 
nice_name(status) 

助手的明显优势是它可以优雅地处理 nil 对象,保持视图干净。缺点是代码现在位于远离模型的不同位置 从

性能角度来看,您不会看到使用帮助程序和模型之间有任何显着差异。拉取状态对象的数据库往返更有可能成为瓶颈。

This is really subjective and I agree, sometimes it is not clear if something belongs in a model or helper.

For example:

# using model
status ? status.nice_name : "Pending" 
# using helper 
nice_name(status) 

The clear advantage here for the helper is that it can handle nil objects gracefully keeping views clean. The disadvantage is that the code is now in a different location away from the model

Performance wise you will not see any significant difference between using helpers and models. It is more likely that DB round trips to pull status objects will be a bottleneck.

献世佛 2024-09-06 17:48:11

在这种情况下我使用常量哈希。

哈希在模型文件中定义,如下所示

STATUS = {
 1 => "Pending",
 2 => "Confirmed" 
}

我还为每个状态声明常量,如下所示。

ST_PENDING = 1

在编写查询时声明这一点很有用。例如,

MyModel.all(:status=>ST_PENDING)

数据库表中的状态字段是数字。所以在打印时,我就简单地使用这个。

MyModel::STATUS[obj.status]

I use constant hashes in this kind of situations.

Hash is defined in model file like this

STATUS = {
 1 => "Pending",
 2 => "Confirmed" 
}

I also declare constants for each status like this.

ST_PENDING = 1

Declaring this is useful when writing queries. For example,

MyModel.all(:status=>ST_PENDING)

status field in database table is number.So when printing, I simply use this.

MyModel::STATUS[obj.status]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文