composed_of Rails中 - 何时使用它? [英] composed_of in Rails - when to use it?

查看:121
本文介绍了composed_of Rails中 - 何时使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你应该使用ActiveRecord的<一个href="http://apidock.com/rails/ActiveRecord/Aggregations/ClassMethods/composed_of"><$c$c>composed_of类方法?

When should you use ActiveRecord's composed_of class method?

推荐答案

我个人认为,当你有没有存储在数据库中的对象,这是有用的,如在数据库中,如:高温,GPS定位,平衡等。

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,

对于例如。

  1. 在温度升高的情况下,我们可能需要像 to_fahrenheit to_celsius ,<$ C方法$ C> is_boiling_point?等

在GPS定位的情况下,我们可能需要像 distance_from方法(点) route_to(点)

in the case of gps location, we might need methods like distance_from(point), route_to(point), etc

所以,当我们只需要创建类的这些对象,并使用composed_of在飞行中初始化这些对象是pretty的有用

so 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

希望它有助于=)

这篇关于composed_of Rails中 - 何时使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆