Question

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!

Was it helpful?

Solution

validates :title, inclusion: TOPICS

or

validates_inclusion_of :title, in: TOPICS
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top