简体   繁体   English

ActiveRecord聚合和form_for

[英]ActiveRecord aggregations and form_for

I'm contemplating about using an ActiveRecord aggregator for some fields. 我正在考虑对某些字段使用ActiveRecord聚合器。

The thing that bothers me that how well do aggregated attributes work with form_for and input fields. 令我困扰的是聚合属性在form_for和input字段中的表现如何。 That is, how do you generate the input fields for the aggregated attributes (since they are read only)? 也就是说,如何生成聚合属性的输入字段(因为它们是只读的)?

Like, lets take the example from http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html . 像这样,以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

Now, lets say we had a form, where customer would be allowed to input his own balance. 现在,假设我们有一个表格,允许客户输入自己的余额。 How do you make that form_for and generate the input fields for balance? 您如何制作form_for并生成用于平衡的输入字段? Also, where do validations for balance belong to? 另外,余额验证在哪里? Does mass assigment work? 大规模归案有效吗?

Mass assignment works (through the normal attribute writer methods), but apparently it isn't ran through the aggregator class's initialize method. 批量分配可以正常工作(通过普通的属性编写器方法),但是显然,它不是通过聚合器类的initialize方法运行的。

Validations also can be normally set on the Customer class. 验证通常也可以在Customer类上设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM