millisami
millisami

Reputation: 10161

Rails environment specific validation

I'm using this plugin called validates-existence-of-uri in my model as follows:

class Project < ActiveRecord::Base
  validates_uri_existence_of :link, :allow_redirect => false
end

Its working and fine. And I've a seeds.rb file which seed the data using Faker to generate urls So, while seeding the data, the above validation tries to check that fake url and I got to comment that validation while seeding data.

So, its annoying me every time I do the seeding. Is there any way out to perform that validation only if the Rails environment is production??

Upvotes: 0

Views: 814

Answers (1)

Ben Crouse
Ben Crouse

Reputation: 8348

To strictly answer your question:

if Rails.env.production?
  validates_uri_existence_of :link, :allow_redirect => false
end

BUT PLEASE DON'T DO THIS. Its a total hack, I think you'd be better off figuring out a better way to generate test urls, maybe some simple like:

VALID_URLS = %w(http://google.com http://yahoo.com http://stackoverflow.com)

...

Project.create(:uri => VALID_URLS.rand)

ActiveSupport adds a rand instance method to Array that returns a random element.

Upvotes: 2

Related Questions