2

I'm currently having an issue getting a running total for a some data to be used on a Rails app with Chartkick and acts_as_taggable_on.

I want my users to search for items with tags, where my @items is:

@items = current_user.items.tagged_with(params[:query])

My x-axis is the created_at date for my items and my y axis is just a :result from my model, which is an integer. I want each item on the x-axis to be a running total including the last value (a positive or negative number).

If i set:

@chart = current_user.items.all

and use this for chartkick:

= line_chart @chart.group_by_day(:created_at).order("day asc").sum(:result).map { |x,y| { x => (@sum += y)} }.reduce({}, :merge)

The chart works great, BUT it's obviously not filtering what's being charted with my search params.

As soon as I change @chart to @items for my line chart which filters via the search params I get the error:

ERROR:  column reference "created_at" is ambiguous.

I've tried creating different scopes in my model to try to help the situation but it doesn't seem to help thus far.

I'd really appreciate any help.

4

1 回答 1

2

好吧,伙计们,我终于解决了这个问题!

大多数关于 PG 冲突的答案都是指 .group(),而我正在查看 .group_by_day(:created_at)。我通过简单地使用修复了模棱两可的错误:

= line_chart @items.group_by_day('items.created_at').order("day asc").sum(:result).map { |x,y| { x => (@sum += y)} }.reduce({}, :merge)

唯一的区别是将 'items.created_at' 作为字符串传递给 group_by_day 以定义我指的是哪个 created_at。

于 2015-03-05T08:59:54.463 回答