Ruby on Rails-总和计算不适用于模型中的关联 [英] Ruby on Rails - Sum calculation not working with associations in model

查看:70
本文介绍了Ruby on Rails-总和计算不适用于模型中的关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户表,其中的列 total_user_xp 具有 has_many:goals 关联。

I have a User table with a column total_user_xp with a has_many :goals association.

目标表中的列 total_goal_xp 用以下方法在模型中计算:

The Goal table has a column total_goal_xp which is calculated in the model with the following method:

# goal.rb

  belongs_to :user
  has_many :goal_activities
  has_many :activities, through: :goal_activities

def total_goal_xp
    self.total_goal_xp = self.goal_activities.sum(:total_xp)
end 

由于某种原因,在我的User控制器和模型文件中,我无法计算 total_goal_xp 的总和,其中user_id与当前用户。它总是返回值0,就像没有将total_goal_xp存储在表中一样。

For some reason, in my controller and model file for User, I can't calculate the sum for total_goal_xp where the user_id matches the current_user. It always returns the value 0 as if the total_goal_xp wasn't being stored in the table.

我对Rails还是很陌生,但是我感觉到我的 total_goal_xp 方法未将值保存到数据库。似乎每次调用该方法都会重新计算总和。就是说,在Rails控制台中,运行Goal.all给了我一个表,该表的值正确,因此我不明白为什么以下方法返回值0。

I'm fairly new to rails but I have a feeling my total_goal_xp method isn't saving the value to the db. It seems to recalculate the sum every time that method is called. That said, in the rails console, running Goal.all gives me a table which does have the correct values so I don't understand why the following methods return the value 0.

user_controller.rb

user_controller.rb

def show
    @user_goals = current_user.goals.all
    @user_xp = @user_goals.sum(:total_goal_xp)
end

user.rb

has_many :goals

def set_total_user_xp
  goal = self.goals.all
  self.total_user_xp = goal.sum(:total_goal_xp)
end

在服务器日志中,以下SQL- SELECT SUM( goals。 total_goal_xp)FROM goals WHERE goals。 user_id =吗? [[ user_id,1]] 这两种方法。

In the server log, I get the following SQL - SELECT SUM("goals"."total_goal_xp") FROM "goals" WHERE "goals"."user_id" = ? [["user_id", 1]] for both methods.

我知道我不应该在控制器中进行计算,但是在此刻,我只是想让计算生效,所以我知道这些关联正在起作用。

I know I shouldn't be doing the calculations in the controller but at the moment, I just want to get the calculation to work so I know the associations are working.

对于保存<$ c $的最佳做法的任何建议,我们将不胜感激。 c> total_goal_xp ,并对total_user_xp做同样的事情。

Would appreciate any advice on the best practice for saving the total_goal_xp and the doing the same for the total_user_xp.

让我知道您是否还想查看其他文件。

Let me know if you'd like to see any other files.

推荐答案

首先,您定义的方法名称与属性/列名称相同,这是冲突的。
其次,如果您要在旅途中计算总数,则不需要这些列。

Firstly, you have defined method name same as attribute/column name, that's a conflict. Secondly, if you want to calculate the total on the go, you don't need those columns.

我将解决此问题的一种方法是当前,请使用此 answer 中提到的回调来更改值时设置总值

One way I would solve this as the columns are present, set the total value when the values are changed using callback mentioned in this answer

def set_total_xp
  goal_activity = self.goal_activities.where(goal_id: goal_id).first_or_initialize
  goal_activity.update_attribute(:total_xp, (quantity*goal.xp))
  goal = Goal.find_by_id(goal_id)
  user = goal.user
  goal.update_attribute(:total_goal_xp, goal.goal_activities.sum(:total_xp))
  user.update_attribute(:total_user_xp, user.goals.sum(:total_goal_xp))
end

或者您甚至不需要每次都重新计算总和

OR you don't even need to recalculate sum everytime

def set_total_xp
  goal_activity = self.goal_activities.where(goal_id: goal_id).first_or_initialize
  goal_activity.update_attribute(:total_xp, (quantity*goal.xp))
  goal = Goal.find_by_id(goal_id)
  xp = goal_activity.total_xp
  goal.update_attribute(:total_goal_xp, (goal.total_goal_xp + xp))
  goal.user.update_attribute(:total_user_xp, (user.total_user_xp + xp))
end

现在,您不需要那些方法,并且在显示数据时,也不会查询数据库。

Now you don't need those methods and also, when displaying the data, there's no query hit to the db..

这篇关于Ruby on Rails-总和计算不适用于模型中的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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