Ruby on rails 如何在rails中显示父记录及其子记录的总数 类附件打开迁移添加编辑它 rails g migration AddAttachmentsCountToDealTask attachments_count:integer

Ruby on rails 如何在rails中显示父记录及其子记录的总数 类附件打开迁移添加编辑它 rails g migration AddAttachmentsCountToDealTask attachments_count:integer,ruby-on-rails,parent-child,counter-cache,Ruby On Rails,Parent Child,Counter Cache,我有父表DealTask和子表Attachment 我想要一个包含相关附件总数的DealTask记录列表 class Attachment < ActiveRecord::Base belongs_to :user, foreign_key: :creator_id belongs_to :deal_task, foreign_key: :relation_id end class DealTask < ActiveRecord::Base has_many :atta

我有父表DealTask和子表Attachment

我想要一个包含相关附件总数的DealTask记录列表

class Attachment < ActiveRecord::Base
  belongs_to :user, foreign_key: :creator_id
  belongs_to :deal_task, foreign_key: :relation_id
end
class DealTask < ActiveRecord::Base
   has_many :attachments, foreign_key: :relation_id
end
试试这个

class Attachment < ActiveRecord::Base
  belongs_to :user, foreign_key: :creator_id
  belongs_to :deal_task, foreign_key: :relation_id
end
class DealTask < ActiveRecord::Base
   has_many :attachments, foreign_key: :relation_id
end

DealTask.first.attachments.count#这将给出attachemenets的计数

DealTask.all.map { |deal_task| deal_task.attachments.ids }.count

为了更好的格式

DealTask.joins(:attachments).select("deal_tasks.*, count(attachements.id) as count").group("deal_tasks.id")

当您在一个查询中获取所有数据时,这将快得多

DealTask.first.attachments.count#这将给出attachemenets的计数

DealTask.all.map { |deal_task| deal_task.attachments.ids }.count
DealTask.joins(:attachments)
        .select("deal_tasks.id, deal_tasks.name, count(attachements.id) as attachments")
        .group("deal_tasks.id")
        .collect(&:attributes)
#This will gve you something like

[
{"id"=>34332630, "name"=>"some name", "attachments"=>1}, 
{"id"=>71649461, "name"=>"some name", "attachments"=>1}
]

为了更好的格式

DealTask.joins(:attachments).select("deal_tasks.*, count(attachements.id) as count").group("deal_tasks.id")
当您在一个查询中获得所有数据时,这将快得多

DealTask.joins(:attachments)
        .select("deal_tasks.id, deal_tasks.name, count(attachements.id) as attachments")
        .group("deal_tasks.id")
        .collect(&:attributes)
#This will gve you something like

[
{"id"=>34332630, "name"=>"some name", "attachments"=>1}, 
{"id"=>71649461, "name"=>"some name", "attachments"=>1}
]
或者,如果您不关心无关访问,也不介意拥有所有的
DealTask
属性,那么您可以在一行中使用编写:

DealTask.all.map do |deal_task|
  deal_task.
    attributes.
    with_indifferent_access.
    slice(:id, :name).
    merge!(total_attachment: deal_task.attachments.count)
end
分解它

DealTask.all.map{|deal_task| deal_task.attributes.merge!(total_attachments: deal_task.attachments.count)}
将返回一个
数组
数组将包含
do
块的结果

DealTask.all.map do |deal_task|
  ...
end
在可以使用
字符串
符号
访问的哈希中为您提供每个
处理任务
的属性(因此称为“无关访问”)

仅保留
交易任务的
:id
:name

deal_task.
  attributes.
  with_indifferent_access.
  slice(:id, :name)
使用键
total\u attachments
将附件计数添加到哈希中

结果应该类似于:

merge!(total_attachments: deal_task.attachments.count)
或者,如果您不关心无关访问,也不介意拥有所有的
DealTask
属性,那么您可以在一行中使用编写:

DealTask.all.map do |deal_task|
  deal_task.
    attributes.
    with_indifferent_access.
    slice(:id, :name).
    merge!(total_attachment: deal_task.attachments.count)
end
分解它

DealTask.all.map{|deal_task| deal_task.attributes.merge!(total_attachments: deal_task.attachments.count)}
将返回一个
数组
数组将包含
do
块的结果

DealTask.all.map do |deal_task|
  ...
end
在可以使用
字符串
符号
访问的哈希中为您提供每个
处理任务
的属性(因此称为“无关访问”)

仅保留
交易任务的
:id
:name

deal_task.
  attributes.
  with_indifferent_access.
  slice(:id, :name)
使用键
total\u attachments
将附件计数添加到哈希中

结果应该类似于:

merge!(total_attachments: deal_task.attachments.count)

我找到了解决亲子关系的最佳方案

[
  {id: 1, name: 'name1', total_attachments: 12},
  {id: 2, name: 'name2', total_attachments: 3}
]
因为上面所有的查询都需要花费太多的时间从数据库加载

所以你们都喜欢用这个

1->在父表中添加一列,称为DealTask

counter_cache: true
2->打开迁移添加编辑它

rails g migration AddAttachmentsCountToDealTask attachments_count:integer
但是是的,重置计数器时必须使用类名,如

find_each, DealTask.all.each do...end
3->设置计数器缓存

DealTask.reset_counters
如果您想要自己的列名,则必须在计数器缓存中指定该列名

假设列名的子任务总数大于

sub_tasks_count
并相应地进行更改以更新计数器缓存

现在,当您添加任何附件时,附件计数列将增加1,这将由**计数器缓存自动完成

有一个问题 **删除任何子计数器时,缓存无法减少**

因此,对于该解决方案,请进行回调

belongs_to :deal_task, foreign_key: :relation_id, counter_cache: :total_subtasks
类附件
因此,当您删除任何附件时,它将通过关系id重置其父级的countet\u缓存,该关系id是附件的父级id或外键

更多信息
请参阅上的视频

我找到了父子关系计数的最佳解决方案

[
  {id: 1, name: 'name1', total_attachments: 12},
  {id: 2, name: 'name2', total_attachments: 3}
]
因为上面所有的查询都需要花费太多的时间从数据库加载

所以你们都喜欢用这个

1->在父表中添加一列,称为DealTask

counter_cache: true
2->打开迁移添加编辑它

rails g migration AddAttachmentsCountToDealTask attachments_count:integer
但是是的,重置计数器时必须使用类名,如

find_each, DealTask.all.each do...end
3->设置计数器缓存

DealTask.reset_counters
如果您想要自己的列名,则必须在计数器缓存中指定该列名

假设列名的子任务总数大于

sub_tasks_count
并相应地进行更改以更新计数器缓存

现在,当您添加任何附件时,附件计数列将增加1,这将由**计数器缓存自动完成

有一个问题 **删除任何子计数器时,缓存无法减少**

因此,对于该解决方案,请进行回调

belongs_to :deal_task, foreign_key: :relation_id, counter_cache: :total_subtasks
类附件
因此,当您删除任何附件时,它将通过关系id重置其父级的countet\u缓存,该关系id是附件的父级id或外键

更多信息
请参见

Hi上的视频,欢迎来到StackOverflow。请参阅关于如何提出适当问题并根据指导原则改进您的问题。作为第一步,请添加您已经尝试过的代码,并描述它如何不适用于您。DealTask.all.map{| deal|u task | Attachment.where(relationship|id:deal_task.id).count}我尝试过这个方法,但它只提供count,而不是完整的父记录您希望返回值是多少?一个
散列
?某种
对象
?(如果是
对象
,属于什么
?)(请为原始问题添加答案,而不是添加注释。)比如什么?您的原始问题没有添加任何内容。您好,欢迎来到StackOverflow。请参阅关于如何提出适当问题并根据指导原则改进您的问题。作为冷杉