如何在表中搜索类似记录并显示计数 - Ruby on Rails [英] How do a search a table for similar records and displaying count - Ruby on Rails

查看:47
本文介绍了如何在表中搜索类似记录并显示计数 - Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我正在构建的 Ruby on Rails 应用程序中创建了一个表,称为标签.这是一个博客应用程序,因此我允许用户将标签与帖子相关联,并通过 :posts、:has_many => 标签和 Tag Beings_to :post 关联来完成此操作.

I have created a table in my Ruby on Rails application that I am building called Tags. It is a blog application so I allow the user to associate tags with a post and do this through a :posts, :has_many => tags and Tag belongs_to :post association.

现在我有了我的标签表,我想看看我将如何呈现视图,以便它显示标签和标签计数.(应该注意的是,我试图在/views/posts/index.html.erb 文件中呈现它).

Now that I have my Tags table I am trying to see how I would render the view such that it displays the tag and tag count. (it should be noted that I am trying to render this in the /views/posts/index.html.erb file).

例如,如果 tag_name Sports 的 Tag 表中有 10 个条目.如何在视图中显示 Sports (10).我不想为特定标签执行此操作,而是以某种方式搜索表格,组合类似标签并显示所有标签的列表,并在它们旁边显示计数.(我真的希望这些是包含该标签的帖子列表的链接,但我很早就学会了一次只问一个问题).

For instance, if there are 10 entries in the Tag table for tag_name Sports. How can I display Sports (10) in the view. I am not looking to do this for a specific tag but rather, somehow search the table, combine like tags and display a list of all tags with counts next to them. (I really want these to be a link to a list of posts that contain that tag but I learned early on only to ask one question at a time).

希望这是有道理的.

评论更新

查看

<% @tag_counts.each do |tag_name, tag_count| %>
    <tr>
      <td><%= link_to(tag_name, posts_path(:tag_name => tag_name)) %> </td>
      <td>(<%=tag_count%>)</td>
    </tr>
<% end %>

postsController:

postsController:

def index
    @tag_counts = Tag.count(:group => :tag_name)
    @posts = Post.all :order => "created_at DESC"



    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
      format.json { render :json => @posts }
      format.atom
    end
  end

推荐答案

执行以下操作:

Tag.count(:group => :name).each do |tag_name, tag_count|
  puts "tag_name=#{tag_name}, tag_count=#{tag_count}"
end

如果在 tags 表中的 name 列上添加索引,可能会提高性能.

You might improve the performance if you add an index on thename column in the tags table.

要显示与标签名称关联的帖子,请执行以下操作:

To display the posts associated with a tag name do the following:

在控制器方法中设置计数哈希:

在与显示标签名称的视图关联的控制器操作中设置 @tag_counts.

Set @tag_counts in the controller action associated with the view displaying the tag names.

@tag_counts = Tag.count(:group => :tag_name)

在视图中将每个标签显示为一个链接:

<% @tag_counts.each do |tag_name, tag_count| %>
  <%= link_to(tag_name, posts_path(:tag_name => tag_name)) %> (<%=tag_count%>)
<% end %>

该链接指向您的 PostsController 的 index 方法.每个链接都有一个 tag_name 参数.

The link points to the index method of your PostsController. Each link has a tag_name parameter.

PostsControllerindex方法中:

In the index method of PostsController:

class PostsController < ApplicationController
  def index
    @tag_counts = Tag.count(:group => :tag_name)
    conditions, joins = {}, nil
    unless (params[:tag_name] || "").empty?
      conditions = ["tags.tag_name = ? ", params[:tag_name]]
      joins = :tags
    end
    @posts=Post.all(:joins => joins, :conditions => conditions)
  end
end

编辑更新了代码以将 name 字段更改为 tag_name.

Edit Updated the code to change the name field to tag_name.

这篇关于如何在表中搜索类似记录并显示计数 - Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆