【发布时间】:2012-12-12 15:12:45
【问题描述】:
我正在编写一个作为定制测量表单的应用程序。
customers 模型有很多属性以毫米为单位的整数形式存储在数据库中。 由于此应用程序将在欧洲和美国使用,因此我将使用虚拟属性向用户显示英寸和厘米版本的数据。
例如,对于我的模型中的客户身高:
def height_in_cm
height / 10
end
def height_in_cm=(height)
self.height = height.to_f * 10
end
def height_in_in
height * 0.039370
end
def height_in_in=(height)
self.height = height.to_f / 0.039370
end
这在我的 _form 视图中:
<% if @customer.measure_unit.eql? "imperial" %>
<%= f.input :height_in_in %></br>
<% else %>
<% if @customer.measure_unit.eql? "metric" %>
<%= f.input :height_in_cm %></br>
<% end %>
<% end %>
正如我所说,我有很多属性,我的客户模型文件变得非常长并且非常容易出错。
有没有干燥的方法可以缩短它?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 dry