Ruby on rails 4 RubyonRails-组帖子和总价

Ruby on rails 4 RubyonRails-组帖子和总价,ruby-on-rails-4,select,grouping,Ruby On Rails 4,Select,Grouping,在我的应用程序中,我有模型访问和发布& class Visit < ActiveRecord::Base belongs_to :post, :counter_cache => true class Post < ActiveRecord::Base has_many :visits 这给了我每个post\u id,但是price只是*BigDecimal:7fb2625f9238,'0.15E1*&我无法通过执行n.post.title和n.post.title找

在我的应用程序中,我有模型
访问
发布
&

class Visit < ActiveRecord::Base
  belongs_to :post, :counter_cache => true

class Post < ActiveRecord::Base
  has_many :visits
这给了我每个
post\u id
,但是
price
只是*BigDecimal:7fb2625f9238,'0.15E1*&我无法通过执行
n.post.title
n.post.title
找到文章标题。title给了我错误:
未定义的方法“post”

这是我得到的结果:

这个选项给我所有的职位和价格单独,而不是合并

结果如下:

我还尝试了:

- Visit.select([:post_id, :price]).where(user: current_user).each do |e|
  %p
    = e.post.title
    = e.cpc_bid
这个选项只给了我一次访问的价格

结果如下:

我最后一次尝试是:

- Visit.joins(:post).group(:post_id).select('sum(price) as earnings', :post_id, :title, 'count(visits.id) as total_views').where(user: current_user).each do |e|
  %p
    = e.title
    = e.price
这给了我一个错误:

PG::GroupingError: ERROR:  column "posts.title" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...ECT sum(price) as earnings, "visits"."post_id", "title", c...

如何将它们与所有帖子的
price
和帖子标题结合起来。

您需要连接表和组

Post.joins(:visits).group(:id)
    .where(visits: { user_id: current_user.id})
    .select("*, sum(price) as total_price, count(visits.id) as total_views")

它增加了post-instance访问器
total_price
total_views

来自@MikDiet的回答非常有效,我遇到的唯一问题是我从
PostgreSql
得到了错误:

PG::GroupingError: ERROR:  column "posts.title" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...ECT sum(price) as earnings, "visits"."post_id", "title", c...
我把它改成:

Visit.joins(:post).group([:post_id, :title]).select('sum(cpc_bid) as earnings', :post_id, :title, 'count(visits.id) as total_views').where(influencer: current_user)
它成功了


注:没有@MikDiet的帮助,我找不到答案,所有的功劳都归他了

谢谢@MikDiet。它在我的
Development ENV
中工作,我的数据库是
sqlite3
。在
Production ENV
中,我使用了
PostgreSql
,我得到了这个错误:
PG::GroupingError:error:column“visions.id”必须出现在GROUP BY子句中,或者在聚合函数中使用
sum(visions.id)
不应该
be
count(visions.id)
,因为
sum
将对所有
ID
s进行求和而不计算它们?这将给我所有的帖子,我只想显示用户访问过的帖子,这就是为什么我有
where(user:current\u user)
。我在哪里可以添加这个?提前感谢!是的,我打错了,应该有
count(visions.id)
更新为
当前用户
- Visit.joins(:post).group(:post_id).select('sum(price) as earnings', :post_id, :title, 'count(visits.id) as total_views').where(user: current_user).each do |e|
  %p
    = e.title
    = e.price
PG::GroupingError: ERROR:  column "posts.title" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...ECT sum(price) as earnings, "visits"."post_id", "title", c...
Post.joins(:visits).group(:id)
    .where(visits: { user_id: current_user.id})
    .select("*, sum(price) as total_price, count(visits.id) as total_views")
PG::GroupingError: ERROR:  column "posts.title" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...ECT sum(price) as earnings, "visits"."post_id", "title", c...
Visit.joins(:post).group([:post_id, :title]).select('sum(cpc_bid) as earnings', :post_id, :title, 'count(visits.id) as total_views').where(influencer: current_user)