mercredi 30 septembre 2015

Rails, route time has to be greater than the time between two stops

I need some help here with a restriction in Rails. I created a transport site and the duration of the whole route should be greater than the time between two stops. This is my model file 'section.rb'

section.rb

class ValidatoreOffset < ActiveModel::Validator
    def validate(record)
        if record.controllo_offset
            record.errors[:base] << 'Error! Orario fermate intermedie maggiore dell orario della tratta di percorrenza'
        end
    end
end




class Section < ActiveRecord::Base
    has_many :departures
    has_many :reservations
    has_many :stops
    attr_accessible :travel_time, :location_arrival, :type_of_section, :start_location, :id, :id_departure, :stop_id
    validates :type_of_section, inclusion: { in: %w(festivo feriale scolastico giornaliero)} , :allow_nil => false
    delegate :offset, :to => :stop, prefix: true, :allow_nil => true

    validates_with ValidatoreOffset

    def controllo_offset
        if section.stop.offset >= section.travel_time
            return true
        end 
    end

end

offset is an attribute of Stop that indicates the time between two stops. What is wrong with that?

Aucun commentaire:

Enregistrer un commentaire