我正在Ruby Rails中创建一个调度系统。该系统由客户,资源和预订组成。预订客户和资源有很多不同之处。客户和资源可以有很多预订。Rails - 已验证的受保护属性?
预订了customer_id和booking_resource_id我认为应该受到保护。我还使用booking_resource作为模型,因为Resource与activeadmin冲突。
我正在使用validates_overlap gem,它允许轻松地创建一个与范围重叠验证:booking_resource_id(https://github.com/robinbortlik/validates_overlap)。目标是我们永远不能同时安排同一资源。
整个事情在批量分配下工作,但只要我将booking_resource_id设置为受保护状态,就可以在控制器中添加各个分配,并通过验证。
如何验证受保护的属性?
我读http://www.davidverhasselt.com/2011/06/28/5-ways-to-set-attributes-in-activerecord/但我似乎有点走投无路。如果我使用属性=和覆盖质量分配保护点是什么?
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
我可以在这里偷步......看样子设置@ booking.customer_id =参数[:预订] [:CUSTOMER_ID]不验证,我没有看到它,因为我没有正确的重定向失败的控制器,刚刚设置为重定向回到索引。 –