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!

1 Answer 1

2
validates :title, inclusion: TOPICS

or

validates_inclusion_of :title, in: TOPICS
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.