When should you use ActiveRecord's composed_of
class method?
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- Is there a way to remove IDV Tags from an AIFF fil
-
undefined method `type_cast' for #
相关文章
- mongodb-aggregate聚合查询分组后如何获得多字段
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
- Rspec controller error expecting <“index”> but
A more complex example of how to use
composed_of
with Money:Source: github wiki.
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 =)