composed_of Rails中 - 何时使用它?使用它、composed_of、Rails

2023-09-09 22:05:09 作者:别介意、这是我的本性

当你应该使用ActiveRecord的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,

对于例如。

在温度升高的情况下,我们可能需要像 to_fahrenheit to_celsius , 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

希望它有助于=)