0

I have two models lecture and students in rails. A lecture can have many students and a students can have many lectures. So for that I have used has_and_belongs_to_many.

But how can I implement a constrain that there should be no conflicting lectures(due to start time and end time) for a student.

Both the models are joined by their primary key.

Models of lectures and students are

create_table :lectures do |t|
  t.string :title
  t.datetime :start_time
  t.datetime :end_time

--

create_table :student do |t|
  t.string :name
sbak
  • 43
  • 7
  • it will be better if you can upload your sample code so others can help you – widjajayd May 04 '20 at 14:49
  • @widjajayd Done – sbak May 04 '20 at 14:51
  • 1
    You likely want to prevent it in Rails before trying to add it with something like `before_save :prevent_overlapping_lectures`. Adding the actual constraint could help prevent race conditions though. However, you can't just add a subquery to a constraint, so you'd likely need to build a custom SQL function to check against as shown here: https://stackoverflow.com/a/11780644/23915 – Unixmonkey May 04 '20 at 15:27

0 Answers0