2011-05-16 67 views
0

我正在尋找刪除任何正在顯示的重複標籤,並在索引頁上顯示最多10個標籤。有關我如何做這件事的任何建議?Rails 3 - 限制文章標籤

/控制器/ 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

要avoir複製和發表日期順序,你的標籤型號:

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

要在您的控制器中取10個第一個標籤:

@tags = Tag.limit(10).all 

瞧!

+0

獨特性和作品。 * @ tags = Tag.limit(10).all *沒有任何作用?! – ubique 2011-06-02 18:50:03