model trip 字段 start_date、end_date、user_id
目前需要 验证 同一 user 不同记录的 start_date -> end_date 是互斥的 目前我 采用的是 before_filter : date_validate, :only => [:create , :update ] 方式过滤掉
此处有误: unless (@start_date.to_date < trip.start_date && @end_date.to_date < trip.start_date) ||(@start_date.to_date > trip.end_date && @end_date.to_date >=trip.end_date) 应该为: unless (@start_date.to_date < trip.start_date && @end_date.to_date < trip.start_date) ||(@start_date.to_date > trip.end_date && @end_date.to_date > trip.end_date) 已更正
代码如下:
private
def find_date
@start_date = params[:trip][:start_date]
@end_date = params[:trip][:end_date]
end
def date_validate
if validate_date || date_has_exist
flash[:notice] = l( :start_date_can_not_greater_end_date_or_date_has_exist )
redirect_to(:action => 'index')
else
return true
end
end
def validate_date
find_date
bool = false
if (@start_date > @end_date)
bool = true
end
bool
end
def date_has_exist
find_date
bool = false
trips= User.current.trips(:order => ' id desc')
arr = []
if params[:trip][:id]
trip = Trip.find(params[:trip][:id])
arr << trip
trips = trips - arr
end
if trips.present? && @start_date.present? && @end_date.present?
trips.each do | trip|
unless (@start_date.to_date < trip.start_date && @end_date.to_date < trip.start_date) ||(@start_date.to_date > trip.end_date && @end_date.to_date > trip.end_date)
bool = true
break
end
end
end
bool
end