我有一个这样的模型:
class Transaction < ActiveRecord::Base
def amount
self[:amount].abs
end
def transaction_type
read_attribute(:amount) > 0 ? :credit : :debit
end
def transaction_type=(type)
if type == :credit || type == 'credit'
self[:amount] = amount.abs
elsif type == :debit || type == 'debit'
self[:amount] = amount.abs * -1
else
raise ArgumentError.new 'Type must be credit or debit'
end
end
end
因为我希望我的金额列在呈现时始终为正数。问题显然是视图从来没有调用过这个方法:
<% form_for @transaction do |f| %>
<%= f.error_messages %>
<p>
<%= f.label 'Where?' %><br />
<%= f.text_field :target %>
</p>
<p>
<%= f.label 'What?' %><br />
<%= f.text_field :memo %>
</p>
<p>
<%= f.label 'How much?' %><br />
<%= f.text_field :amount %>
</p>
<p>
<%= f.radio_button :transaction_type, 'debit' %>
<%= f.label :transaction_type_debit, 'Debit' %>
<%= f.radio_button :transaction_type, 'credit' %>
<%= f.label :transaction_type_credit, 'Credit' %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
难道我做错了什么?还是有更好的方法来做到这一点?
编辑:添加了我的 transaction_type 访问器方法,这更好地解释了为什么我不在数据库中将金额存储为仅正数。