Rails 3 - 限制文章标签

时间:2011-05-16 18:20:52

标签: ruby-on-rails-3

我希望删除所显示的所有重复标记,并在索引页面上显示最多10个标记。关于我如何做到这一点的任何建议?

/控制器/ tags_controller

class TagsController < ApplicationController
def show
@tag = Tag.limit(10).all
@tag = Tag.find(params[:id])
@articles = @tag.articles
end
end
end

模型/ tag.rb

class Tag < ActiveRecord::Base

validates :name, :uniqueness => true
#default_scope :order => 'created_at DESC'

has_many :taggings, :dependent => :destroy  
has_many :articles, :through => :taggings
end

1 个答案:

答案 0 :(得分:1)

按照发布日期复制和订购,在您的代码模型中

validates :name, :uniqueness => true
default_scope :order => 'created_at DESC'

要在控制器中获取十个第一个标记:

@tags = Tag.limit(10).all

瞧!