我正在考虑在某些领域使用ActiveRecord聚合器。
困扰我的是聚合属性与form_for和输入字段的协作能力。也就是说,如何为聚合属性生成输入字段(因为它们是只读的)?
喜欢,让我们从http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html获取示例。
class Customer < ActiveRecord::Base
composed_of :balance, :class_name => "Money", :mapping => %w(balance amount)
composed_of :address, :mapping => [ %w(address_street street), %w(address_city city) ]
end
class Money
attr_reader :amount, :currency
def initialize(amount, currency = "USD")
@amount, @currency = amount, currency
end
end
现在,假设我们有一个表单,允许客户输入自己的余额。你如何制作form_for并生成平衡的输入字段?此外,平衡验证属于哪里?群众分配是否有效?
答案 0 :(得分:1)
批量分配工作(通过普通的属性编写器方法),但显然它不是通过聚合器类的初始化方法运行的。
也可以在Customer类上设置验证。