1

I'm doing a tags model where one of the attributes is the title of the tag. The goal is that there will be articles that can be tagged by a predefined set of topics:

TOPICS = ['Politics', 'Art', 'Sports', 'Tech', 'Business', 'Science']

I would like to create a uniqueness validation such that whenever a tag is assigned to an article, its title must be any of the elements in TOPICS. Can I do this via the following?

class Tag < ActiveRecord::Base
  validates :title, :uniqueness => { :scope => TOPICS }
end

If not, how do I set TOPICS as the scope for title? Thanks in advance!

Adler Santos
  • 365
  • 5
  • 19

1 Answers1

2
validates :title, inclusion: TOPICS

or

validates_inclusion_of :title, in: TOPICS
Mori
  • 27,279
  • 10
  • 68
  • 73