ubique
ubique

Reputation: 1212

Rails 3 - Restricting Article Tags

I am looking to remove any duplicated tags being displayed and have a maximum number of 10 tags on display on the index page. Any suggestions on how I might do this?

/controller/tags_controller

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

model/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

Upvotes: 0

Views: 64

Answers (1)

Hartator
Hartator

Reputation: 5145

To avoir duplicate and to order by published date, in your tag model :

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

To fetch the ten first tags, in your controller :

@tags = Tag.limit(10).all

Voila!

Upvotes: 1

Related Questions