Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.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 如何优雅地组合这两个ruby命令?_Ruby On Rails_Ruby_Ruby On Rails 3_Acts As Taggable On - Fatal编程技术网

Ruby on rails 如何优雅地组合这两个ruby命令?

Ruby on rails 如何优雅地组合这两个ruby命令?,ruby-on-rails,ruby,ruby-on-rails-3,acts-as-taggable-on,Ruby On Rails,Ruby,Ruby On Rails 3,Acts As Taggable On,我在我的物品模型上使用ActsAsTaggableOn 要获得使用特定标记标记的所有项目的列表,我必须执行以下操作: Item.tagged_with("some_tag") 但是,我在导航部分中使用它,根据标记中的项目数量动态生成标记的导航。我想要4个标签,有最多的项目数 所以我在我的导航部分这样做: 例如: > i => #<Item id: 17, name: "10pp-logo", link: "10pound.zip", description: "10PP L

我在我的物品模型上使用ActsAsTaggableOn

要获得使用特定标记标记的所有项目的列表,我必须执行以下操作:

Item.tagged_with("some_tag")
但是,我在导航部分中使用它,根据标记中的项目数量动态生成标记的导航。我想要4个标签,有最多的项目数

所以我在我的导航部分这样做:

例如:

> i
 => #<Item id: 17, name: "10pp-logo", link: "10pound.zip", description: "10PP Logo.<br><br>Full of graphical good...", price: 6.99, user_id: 1702, created_at: "2013-11-24 12:56:05", updated_at: "2013-11-24 12:56:05", image: "ten.png", is_approved: false, approved_at: nil> 
> i.is_approved?
 => false 
编辑1:


为什么不只列出批准的项目

class Item < ActiveRecord::Base
  def self.approved_and_tagged_with(tags)
    self.tagged_with(tags).where(approved: true)
  end
end

# View
Item.approved_and_tagged_with("some_tag")
更新:包装标记最多的项目

class Item < ActiveRecord::Base
  scope :approved, where(approved: true)

  # @param: options. allow nil
  #   example: most_tagged(limit: 10, require_approval: false)
  def self.most_tagged(options={})
    limit = options[:limit] || 4
    require_approval = options[:require_approval] || true
    results = tag_counts.order(:count).limit(limit)
    results = results.approved if require_approval?
    results
  end
end

# View
Items.most_tagged.each do |item|
  # blah blah
end

method.tag\u counts返回什么?像[[tag_name,count][…]]这样的数组?@tyler-是的,一个数组。一个什么数组?它的实际结构是什么?@tyler更新了这个问题来告诉你。问题是我不是只调用一个标签。我认为你的思路是对的,但我的导航系统不是那样工作的。我想修改迭代器,我正在用导航部分的完整代码更新这个问题。不管你想要多少个标签,也不管你需要做什么其他花哨的事情。只要你需要点击db,你就可以通过包装act_As_taggable_on方法将批准的逻辑组合在一起。因此,理想情况下,我希望能够在遍历tag_count上的所有标记时,只查看批准的标记:true。你能更新你的答案以反映你的建议和我的实际代码吗。谢谢。很好……出于好奇……为什么要包括选项。需要您的批准?是吗?
 > Item.tag_counts
   (2.0ms)  SELECT items.id FROM "items" 
  ActsAsTaggableOn::Tag Load (4.5ms)  SELECT tags.*, taggings.tags_count AS count FROM "tags" JOIN (SELECT taggings.tag_id, COUNT(taggings.tag_id) AS tags_count FROM "taggings" INNER JOIN items ON items.id = taggings.taggable_id WHERE (taggings.taggable_type = 'Item' AND taggings.context = 'tags') AND (taggings.taggable_id IN(13,15,17)) GROUP BY taggings.tag_id HAVING COUNT(taggings.tag_id) > 0) AS taggings ON taggings.tag_id = tags.id
 => [#<ActsAsTaggableOn::Tag id: 6, name: "gmail">, #<ActsAsTaggableOn::Tag id: 11, name: "10pp">, #<ActsAsTaggableOn::Tag id: 4, name: "twitter">, #<ActsAsTaggableOn::Tag id: 5, name: "facebook">, #<ActsAsTaggableOn::Tag id: 8, name: "fitness">, #<ActsAsTaggableOn::Tag id: 1, name: "notepad">, #<ActsAsTaggableOn::Tag id: 2, name: "paper">, #<ActsAsTaggableOn::Tag id: 9, name: "logo">, #<ActsAsTaggableOn::Tag id: 7, name: "login form">] 
class Item < ActiveRecord::Base
  def self.approved_and_tagged_with(tags)
    self.tagged_with(tags).where(approved: true)
  end
end

# View
Item.approved_and_tagged_with("some_tag")
class Item < ActiveRecord::Base
  scope :approved, where(approved: true)

  # @param: options. allow nil
  #   example: most_tagged(limit: 10, require_approval: false)
  def self.most_tagged(options={})
    limit = options[:limit] || 4
    require_approval = options[:require_approval] || true
    results = tag_counts.order(:count).limit(limit)
    results = results.approved if require_approval?
    results
  end
end

# View
Items.most_tagged.each do |item|
  # blah blah
end