Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何通过与:uniq=>;的关系从has_many:获取行计数;真的_Ruby On Rails_Activerecord_Has Many Through - Fatal编程技术网

Ruby on rails 如何通过与:uniq=>;的关系从has_many:获取行计数;真的

Ruby on rails 如何通过与:uniq=>;的关系从has_many:获取行计数;真的,ruby-on-rails,activerecord,has-many-through,Ruby On Rails,Activerecord,Has Many Through,这是我的模型: class Tag < ActiveRecord::Base # id, name has_many :taggings end class Tagging < ActiveRecord::Base # id, tag_id, owner_id, target_type, target_id belongs_to :tag belongs_to :owner, :class_name => 'User' belongs_to :targ

这是我的模型:

class Tag < ActiveRecord::Base
  # id, name
  has_many :taggings
end

class Tagging < ActiveRecord::Base
  # id, tag_id, owner_id, target_type, target_id
  belongs_to :tag
  belongs_to :owner, :class_name => 'User'
  belongs_to :target, :polymorphic => true
  validates_uniqueness_of :tag_id, :scope => [ :target_id, :target_type, :owner_id ]
end

class Asset < ActiveRecord::Base
  # id, owner_id, title, type, etc
  belongs_to :owner, :class_name => 'User'
  has_many :taggings, :as => :target
  has_many :taggers, :through => :taggings, :source => :owner, :uniq => true
  has_many :tags, :through => :taggings, :uniq => true
end

class User < ActiveRecord::Base
  # id, name, email, etc
  has_many :assets, :foreign_key => 'owner_id'
  has_many :my_taggings, :class_name => 'Tagging', :foreign_key => 'owner_id'
  has_many :my_tags, :through => :my_taggings, :source => :tag, :uniq => true
  has_many :taggings, :as => :target
  has_many :taggers, :through => :taggings, :source => :owner, :uniq => true
  has_many :tags, :through => :taggings, :uniq => true
end
调用Asset.find(:first).tags会像预期的那样返回一个标记数组,但我需要每个标记都包含一个count属性,指示如果未指定:uniq=>true,该行将出现多少次


多个用户可以将同一标签应用于一项资产。我想显示标记名加上应用它的用户数。

这应该完全符合您的要求

has_many :tags_with_count, :source => :tag, :through => :taggings, 
  :group => "tags.id", :joins => :taggings,
  :select = "tags.*, COUNT('taggings.id') AS frequency"
就返回的行而言:group=>:id将返回与:uniq=>true相同的集合,但它也允许您执行所需的计算。这条语句比:uniq=>true更耗费人力,因此我给了它一个不同的名称,允许您选择是获取具有分组计数的唯一标记,还是仅获取唯一标记的列表

上述语句将向返回的记录添加frequency属性。通过神奇的方法_missing,您可以使用@tag.frequency访问该方法

用法:

@tags = @asset.tags_with_count
@tags.each{|tag| puts [tag.id, tag.name. tag.frequency].join "\t"}

将打印@asset的每个标记的id、名称和出现次数。

不太有效。我不得不删除:joins=>:标记并添加一个:source=>:标记。下面的工作方式完全符合要求--有很多:带有计数的标记,:通过=>:taggings,:source=>:tag,:group=>“tags.id”,:select=>“tags.*,计数(\“taggings.id\”)作为频率“。。。。。如果没有你的帮助,我不会发现这个,所以我接受你的回答。如果你能修改你的答案就太好了。很抱歉。我应该捕捉到嵌套的引号,并且需要一个源选项。已编辑解决方案以修复这些小错误。
@tags = @asset.tags_with_count
@tags.each{|tag| puts [tag.id, tag.name. tag.frequency].join "\t"}