views:

48

answers:

1

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??

+2  A: 

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.

Ben
Hi Ben,Actually for the testing, I'm using Fakeweb and its great.My concern is while I'm seeding the data via rake db:seed command.While running this command, I want to populate the development env database.Ain't there any other way out??
Millisami
Hmm, sorry I don't think I understand your situation then. Could you post your seeds.rb? Or explain a bit more?
Ben