Tag model

Many-to-Many with has_many :through

I'm working on my new project and it has many-to-many association. Also, I want to store additional fields in the relation. So, I used has_many :through in my models.

Example:

GIVEN: I have 2 models which are driver and bus. Driver can drive more than one bus and has start time and stop time for each bus. Also, bus can be drived by many drivers. ( Driver will drive particular bus only one period)
I will generate one more model called " Shift " which has bus_id, driver_id, start_time and stop_time. 

Model:

class Shift < ActiveRecord::Base
belongs_to :bus
belongs_to :driver
end
class Driver < ActiveRecord::Base
has_many :shifts
has_many :buses, :through => :shifts
end
class Bus < ActiveRecord::Base
has_many :shifts
has_many :drivers, :through => :shifts
end

Then, how to access start and stop time?

We treat Shiftas one model, so we can use
s = Shift.find_by_bus_id_and_driver_id(busID,driverID)
to get the shift and s.start_time or s.stop_time to access the additional fields.

I love Rails.