从(即将变得瘦)控制器转移到(不太胖)模型

发布于 2024-11-04 03:23:49 字数 458 浏览 0 评论 0原文

我几乎不懂 Rails,更不用说 ruby​​,所以这可能是一个简单的问题。在我的控制器的一个显示操作中,我定义了一个变量来计算某些项目的总数,看起来像这样:

@total_of_items = SomeModel.where(:user_id => @user).sum(:amount)

这显然收集了该特定用户的所有金额属性并将它们相加。所以在我看来,我只需调用:

<%= @total_of_items %>

它就会出现。那么两个问题。首先,这真的是做这样的事情的最好方法吗?因为我可能想获取这个值并在另一个用户的视图中显示它以比较两者,或者类似的东西。似乎为了做到这一点,这必须是模型中的一种方法?我只是不确定。

我的第二个问题是如何获取控制器中定义的变量并将其放入模型中。这似乎是更“瘦控制器胖模型”的做事方式。帮助不大?

I barely understand rails, let alone ruby, so this is probably an easy one. In one of my controller's show action I have a variable that I have defined to calculate the total of some items, looks something like this:

@total_of_items = SomeModel.where(:user_id => @user).sum(:amount)

This obviously collects all of the amount attributes of this particular user and adds them up. So in my view I just call:

<%= @total_of_items %>

And it shows up. So two questions. First, is this really the best way to do something like this? Because I may want to take this value and show it in another user's view to compare the two, or something like that. It seems like in order to do that this would have to be a method in the model? I'm just not sure.

My second question is how to take a variable defined in the controller and put it in the model. It seems like that's the more "skinny controller fat model" way of doing things. Little help?

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

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

发布评论

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

评论(2

梅窗月明清似水 2024-11-11 03:23:49

这是范围出现的地方。在你的模型中:

scope :sum_by_user, lambda {|user| where(:user_id => user).sum(:amount) }

然后在你的控制器中:

@total_of_items = SomeModel.sum_by_user(@user)

你甚至可以链接范围,这真的很棒。

http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html

This is where scopes appear. In your model:

scope :sum_by_user, lambda {|user| where(:user_id => user).sum(:amount) }

Then in your controller:

@total_of_items = SomeModel.sum_by_user(@user)

You can even chain scopes which is really great.

http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html

薄荷港 2024-11-11 03:23:49

您可以通过创建一个范围(如 @apneadiving 演示)或通过封装查询的方法来简化控制器:

def self.amount_sum(user)
  where(:user_id => user).sum(:amount)
end

然后在您的控制器中:

@total_of_items = SomeModel.amount_sum(@user)

在这种情况下,我将使用该方法,因为范围有点难以阅读。 Ryan Bates 在他的 Railscast 215 中记录了这一点:

在我们使用的第二个命名范围中
一个 lambda。如果您曾经使用过其中之一
在您可能会考虑的命名范围内
使用类方法代替
特别是如果你传递的是
大量参数或者如果
范围内容复杂。我们的
相当简单,但我们会改变它
无论如何,进入类方法。

您的情况非常简单,但我会考虑将逻辑放在类方法中。

You can simplify your controller by creating a scope (as @apneadiving demonstrates), or by a method that encapsulates the query:

def self.amount_sum(user)
  where(:user_id => user).sum(:amount)
end

then in your controller:

@total_of_items = SomeModel.amount_sum(@user)

In this case I would use the method, because the scope is a little harder to read. Ryan Bates makes note of this in his Railscast 215:

In the second named scope we’re using
a lambda. If you ever use one of these
in a named scope you might consider
using a class method instead
especially if you’re passing in a
large number of parameters or if the
content of the scope is complex. Ours
is fairly simple but we’ll turn it
into a class method anyway.

Your case is pretty simple, but I'd consider placing the logic inside a class method.

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