【发布时间】:2015-06-15 01:24:49
【问题描述】:
我想运行验证,如果预订已存在,则会返回错误消息。
最新更新
它可以工作,但它会导致我使用更新的工作代码提出一个新问题: Rails code readability for my validation
状态更新
当房间已经有预订时,它会触发验证回滚 (已经开始)但现在我想找出一个代码,以便它只 日期重叠时回滚,而不仅仅是因为有预订。
这是我的架构:
create_table "bookings", force: :cascade do |t|
t.datetime "start_date"
t.datetime "end_date"
t.integer "length"
t.integer "room_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "rooms", force: :cascade do |t|
t.string "name"
t.string "type_room"
t.integer "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
我的联想除了预订模式:
class Room < ActiveRecord::Base
has_many :bookings
end
提前感谢大家的帮助。安托万
更新的控制器
def create_book_now
@room = Room.find(params[:room_id])
#Save booking in DB if model validation are OK
booking = @room.bookings.build(booking_params)
if booking.save
booking.update(end_date: booking.start_date + booking.length.days)
flash[:notice] = "Booking done"
redirect_to root_path
else
flash[:error] = booking.errors.full_messages.first if booking.errors.any?
redirect_to room_book_now_path(@room.id)
end
end
更新模型
def dates_are_available
room = Room.find(self.room_id)
# if Room.find(self.room_id).bookings.exists?
# self.errors.add(:base, 'Date already taken')
# end
conditions = []
conditions << '(start_date >= :new_start_date AND end_date >= :new_end_date)'
conditions << '(start_date >= :new_start_date AND end_date <= :new_end_date)'
conditions << '(end_date BETWEEN :new_start_date AND :new_end_date)'
conditions << '(start_date <= :new_start_date AND end_date >= :new_end_date)'
if room.bookings.where(conditions.join(' OR '), new_start_date: self.start_date, new_end_date: self.end_date).exists?
self.errors.add(:base, 'Date already taken')
return false
end
end
现在这个验证工作了,但是它决定不记录控制器中记录的 end_date 而不是抛出错误
【问题讨论】:
-
你的意思是你的验证没有被执行?你怎么知道的?
-
首先,我不会在模型本身中处理重定向……那应该在控制器中。无论如何,您可以添加
logger.debug 'in validation'以查看您的验证是否被调用? -
我知道它没有被执行,因为重定向没有发生,我看到记录保存在数据库中。我故意尝试创建一个应该让验证工作的记录。
-
我的控制器一定有问题吗?我应该告诉控制器它必须在创建预订时运行验证?
-
您的 validate_existing_dates 的 if 语句可能无法正常工作。在 if 语句后添加 binding.pry,看看它是否真的正确触发。
标签: ruby-on-rails validation ruby-on-rails-4 model-view-controller