简体   繁体   中英

ActiveRecord aggregations and form_for

I'm contemplating about using an ActiveRecord aggregator for some fields.

The thing that bothers me that how well do aggregated attributes work with form_for and input fields. 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 .

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? 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.

Validations also can be normally set on the Customer class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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