开发者

ActiveRecord aggregations and form_for

开发者 https://www.devze.com 2023-02-07 16:17 出处:网络
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 ge

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 f开发者_运维百科orm_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.

0

精彩评论

暂无评论...
验证码 换一张
取 消