validates :title, inclusion: TOPICS
or
validates_inclusion_of :title, in: TOPICS
Pregunta
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!
Solución
validates :title, inclusion: TOPICS
or
validates_inclusion_of :title, in: TOPICS