When should you use ActiveRecord’s composed_of class method?
When should you use ActiveRecord’s composed_of class method?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Personally, I think this is useful when you have objects which are not stored in database, as shown in the database, e.g. temperature, GPS location, balance, etc.
You might ask then why those are not stored in the database? In the database we only store a value, but if we want to attach useful, relevant methods to that value,
for e.g.
in the case of temperature, we might need methods like
to_fahrenheit,to_celsius,is_boiling_point?, etcin the case of GPS location, we might need methods like
distance_from(point),route_to(point), etcso it’s pretty useful when we can just create the classes for these objects, and use composed_of to initialize these objects on the fly
hope it helps =)