Time for action — insuring drivers

Let's prepare different types of insurance as follows:

# app/models/pilot.rb
class Pilot < AeroSpace
embeds_many :insurances, as: :insurable
end
# app/models/car_driver.rb
class CarDriver < Terrestrial
embeds_many :insurance, as: :insurable
end
# app/models/astronaut.rb
class Astronaut < AeroSpace
embeds_many :insurances, as: :insurable
end

And now we design the Insurance class as follows:

# app/models/insurance.rb
class Insurance
include Mongoid::Document
embedded_in :insurable, polymorphic: true
end
# app/models/travel_insurance.rb
class TravelInsurance < Insurance
end
# app/models/theft_insurance.rb
class TheftInsurance < Insurance
end

Now let's provide insurance policies for our drivers as follows:

irb> p = Pilot.first
=> #<Pilot _id: 4ef9a410fed0eb977d000002, _type: "Pilot", name: "Gautam", age: nil, address: {"street"=>"asfds", "city"=>"Pune", "_id"=>BSON::ObjectId('4f0491bcfed0ebcc59000001')}, weight: nil, gforce: nil>
irb> p.insurances << TravelInsurance.new
=> [#<TravelInsurance _id: 4f06ad2efed0ebe598000002, _type: "TravelInsurance">]
irb> a = Astronaut.first
=> #<Astronaut _id: 4f069fd8fed0ebe45d000001, _type: "Astronaut", name: nil, age: nil, address: nil, weight: nil, gforce: nil>
irb> a.insurances << TravelInsurance.new
=> [#<TravelInsurance _id: 4f06b058fed0ebe598000004, _type: "TravelInsurance">]
irb> a.insurances << FireInsurance.new
=> [#<FireInsurance _id: 4f06ad6bfed0ebe598000003, _type: "FireInsurance">]
irb> a.insurances
=> [#<FireInsurance _id: 4f06ad6bfed0ebe598000003, _type: "FireInsurance">, #<TravelInsurance _id: 4f06b058fed0ebe598000004, _type: "TravelInsurance">]

What just happened?

Let's have a closer look at the preceding commands:

irb> p = Pilot.first
=> #<Pilot _id: 4ef9a410fed0eb977d000002, _type: "Pilot", name: "Gautam", age: nil, address: {"street"=>"asfds", "city"=>"Pune", "_id"=>BSON::ObjectId('4f0491bcfed0ebcc59000001')}, weight: nil, gforce: nil>
irb> p.insurances << TravelInsurance.new

=> [#<TravelInsurance _id: 4f06ad2efed0ebe598000002, _type: "TravelInsurance">]

Here, Insurance is polymorphic. This means that the Insurance object can be embedded in multiple parents. In this case, we have TravelInsurance (that is, a model, which inherits from Insurance) being assigned to the Pilot class:

irb> a = Astronaut.first
=> #<Astronaut _id: 4f069fd8fed0ebe45d000001, _type: "Astronaut", name: nil, age: nil, address: nil, weight: nil, gforce: nil>
irb> a.insurances << TravelInsurance.new

=> [#<TravelInsurance _id: 4f06b058fed0ebe598000004, _type: "TravelInsurance">]

Now, we have the TravelInsurance object being embedded in the Astronaut class. This shows us the polymorphic nature of the Insurance embedded object — it can be embedded in different parents.

Have a go hero

Why don't you try and assign TheftInsurance to CarDriver?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.166.74