简体   繁体   中英

Composite field in rails model

I have this model in Rails:

class PaymentsDcument < ActiveRecord::Base
  attr_accessible :date, :domain, :id,  :total, :paid, :paid_left, :percentage
end

and I want to add the field payment_total = total*percentage without creating the field in the database.

Some people use a Presenter for this kind of code, but you could go with

class PaymentsDcument < ActiveRecord::Base
  attr_accessible :date, :domain, :id,  :total, :paid, :paid_left, :percentage

  def payment_total
    total*percentage
  end
end

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