ruby-on-railsrubyvalidation

Custom Rails Validation (Doesn't seem to be working)


I've got the following Model for Accounts

require 'net/http'
require 'uri'
require 'date'

class Account < ActiveRecord::Base
validates_presence_of :username, :password, :on => :update
validate :valid_expiry_date, :on => :update  

def valid_expiry_date
   reply = Net::HTTP.get URI.parse("http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=getaccountdetails_v1&type=prem&login=" + username + "&password=" + password)
   account = Time.at(reply[80..90].to_i)

   if (Time.now + 2419200) <= account
     return true      
   else
     return false
     errors.add_to_base("Sorry this account isn't valid") 
   end
 end
end

I know the code works in a ruby.rb file and will return true or false, however I seem to be having rather a lot of difficultly trying to translate this code into an actual validation, any help would be much appreciated. Thanks

Its also defiantly at least connecting to the website as my firewall asked me if the Terminal was allowed to access it.

At the moment not only is it displaying no errors it's actually letting anything through and saving it.


Solution

  • I don't know it solved your problem or not but your 'return statement' should be after the line

       errors.add_to_base("Sorry this account isnt valid") 
    

    Change it to and check again

       if (Time.now + 2419200) <= account
         return true      
       else
         errors.add_to_base("Sorry this account isnt valid") 
         return false
       end