0

Let's say that I have a two models with this schema:

>   create_table "rents", force: :cascade do |t|
t.integer  "car_id"
t.datetime "date_start"
t.datetime  "date_end"
t.integer  "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

  create_table "cars", force: :cascade do |t|
t.string   "name"
t.text     "description"
t.integer  "price_id"
t.datetime "created_at",  null: false
t.datetime "updated_at",  null: false
end

Model rent belongs to car and car has_one rent.

How can I add date validation so let's say there exist a rent with car_id: 2 and start_date like 2015-11-01 and date_end like 2015-11-05. I want application to give me error if I want to add another one rent with car_id: 2 and dates where this car is already booked.

How can I do this? Should I use any particular gem or something?

Rohnin
  • 53
  • 4

2 Answers2

0

You should use Rails Callback for your validation and if not satisfied, raise an error.

0

You can try with this gem:

    gem 'bookable', '~> 0.0.52'

Its documentation is here: https://github.com/kunks001/bookable

Or just try model validation like this one Dealing with time periods in Rails 3.1

Community
  • 1
  • 1