I am creating a scheduling system in Ruby Rails. The system is composed of Customers, Resources, and Bookings. There is many to one with Booking to Customer and Resources. A customer and resource can have many bookings.
Booking has customer_id and booking_resource_id I thought should be protected. I also used booking_resource as the model because Resource conflicted with activeadmin.
I am using the validates_overlap gem that allows easily to create a overlapping validation with a scope to :booking_resource_id ( https://github.com/robinbortlik/validates_overlap) . The goal is we can never schedule the same resource at the same time.
The whole thing works under mass assignment but as soon as I put booking_resource_id as protected, add the individual assignments in the controller the validation is by passed.
How can I validate a protected attributed?
I read http://www.davidverhasselt.com/2011/06/28/5-ways-to-set-attributes-in-activerecord/ but I seem a little bit cornered. If I used attributes= and override mass assignment protected what is the point?
class Booking < ActiveRecord::Base
belongs_to :customer
belongs_to :booking_resource
attr_accessible :approved, :approvedBy, :end, :start, :title
attr_protected :customer_id, :booking_resource_id
validate :start_cannot_be_future
validates :start, :end, :overlap => {:scope => :booking_resource_id}
def start_cannot_be_future
if self.start > self.end
errors.add(:start, "Date can't be in the future")
end
end
end