MKK
MKK

Reputation: 2753

How can I set up reserved word for particular column in validation?

I have a model called Community and it has a column called name

I use this name in sub-domain.

For example, when a user access to http://rockstar.test-sample.com, it show the same content as http://test-sample.com/community/rockstar

obviously, this name shouldn't be www

How can I prohibit www if I'm stating that in models/community.rb?

Upvotes: 2

Views: 609

Answers (1)

mu is too short
mu is too short

Reputation: 434685

You might want to spend some time with the Active Record Validations Guide:

2.4 exclusion

This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.

class Account < ActiveRecord::Base
  validates :subdomain, exclusion: { in: %w(www us ca jp),
    message: "Subdomain %{value} is reserved." }
end

So something like this in your model should do the trick:

validates :name, :exclusion => { in: %w[www] }

Upvotes: 8

Related Questions