在Papertrail中显示单个记录的所有版本 [英] Display all versions of individual records in Papertrail

查看:98
本文介绍了在Papertrail中显示单个记录的所有版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个联赛系统,当前它会根据结果存储和更新球员的"elo得分".现在,我正在尝试添加"HighCharts",以通过漂亮的折线图显示本赛季的球员elo得分.有人建议我使用Papertrail来存储更新,并且已经安装了所有更新.

I'm building a league system and currently it stores and updates the players 'elo score' depending on the result. Now, I'm trying to add in 'HighCharts' to display the players elo score over the season in a sweet looking line chart. Someone suggested I use Papertrail to store the updates and I have got that all installed.

现在这是我的问题,我似乎无法弄清楚如何将用户elo_score版本以易于"HighCharts"使用的数组吐出.我可以获取elo_score的最新更新:

Now here comes my problem, I can't seem to figure out how to spit out the users elo_score versions in an array easy for 'HighCharts' to use. I can get the last updates to elo_score:

Last updated score = <%= @player.versions.last.reify.elo_score %> 

但是我似乎找不到语法来吐出全部的'elo_score'版本.诸如"1000、1020、1043、1020"之类的内容.

But I can't seem to find the syntax to spit out all the 'versions' for 'elo_score'. Something like "1000, 1020, 1043, 1020".

我也尝试过:

<%= @player.versions.map { |version| version.reify.elo_score} %>

但是,这给了我"nil:NilClass的未定义方法'elo_score'".而<%= @ player.versions.map {| version | version.reify%>吐出记录中的所有所有信息,而不仅仅是elo_score.

But this gives me "undefined method `elo_score' for nil:NilClass". While just <%= @player.versions.map { |version| version.reify %> spits out all information in the record and obviously not just the elo_score.

任何人都可以帮忙吗?抱歉,如果我不清楚,我绝对是Rails的新手,这在我的业余时间只是一个有趣的项目,但我感到非常震惊!

Can anyone help? Sorry if I've not made this clear, I'm absolute brand new to rails, and this is just a fun project in my spare time but I'm having a blast!

非常感谢!

推荐答案

您在这里所做的事情:

@player.versions.map { |version| version.reify.elo_score }

将所有这些分数并排成一个数组是完全可以的.因为至少有一个reify为nil,所以您遇到的问题(nil:NilClass东西)即将到来.也就是说,某些版本没有验证.

Is perfectly fine to take all those scores and put them in an array. The problem that you're getting (the nil:NilClass stuff) is coming because at least one reify is nil. That is, that some version doesn't have a reify.

如果每个版本都应该有一个版本号,请确保将其添加为模型验证,并在代码中找到设置版本号的位置,并查看为什么无效.

If each version is supposed to have a reify, be sure to add that as a model validation, and find in your code where the reify is being set and see why it's nil.

如果某个版本可以使用nil验证,则可以采用多种方法来实现,但是简单明了的方法如下所示:

If it's okay for a version to have a nil reify, you could accomplish it a number of ways, but the straightforward and explicit way would look like this:

elo_scores = []
@player.versions.each do |version|
    unless version.reify.nil?
        elo_scores << version.reify.elo_score
    end
end

我建议将其放入类似get_elo_scores的方法中,然后您可以更轻松地像这样调用它:

I would suggest putting this in to a method, like get_elo_scores, and then you could more easily call it like:

@player.get_elo_scores

编辑,请从注释中进行澄清:

EDIT For clarification from the comments:

您的用户模型(或播放器模型,无论您如何命名)都应具有如下所示的方法:

Your User model (or Player model, whatever you named it) should have a method that looks like this:

def get_elo_scores
    elo_scores = []
    self.versions.each do |version|
        unless version.reify.nil?
            elo_scores << version.reify.elo_score
        end
    end
    return elo_scores
end

很抱歉,我没有对此做更清楚的说明,但是您将无法使用此方法访问@player,因为它仅存在于您的控制器和视图的上下文中.上面的代码现在是一个合适的实例方法:它将自己调用.versions,其余的很好.我还在末尾添加了一个显式的回叫.

I apologize for not making this clearer, but you won't have access to @player within this method because that only exists in the context of your controller and view. The above is now a proper instance method: it will call .versions upon itself, and the rest is fine. I also added an explicit return call at the end.

现在,您将可以在任何用户(或播放器)对象上调用@player.get_elo_scores.

Now you will be able to call @player.get_elo_scores on any User (or Player) object.

希望有帮助!

这篇关于在Papertrail中显示单个记录的所有版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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