0
什么是验证关联聚合的好方法。这必须发生访问数据库,这是从模型的属性。我使用的轨道4.示例:验证ActiveRecord关联聚合?
rails g model Donation total:integer
rails g model Donor name amount:integer
class Donation < ActiveRecord::Base
has_many :donors, dependent: :destroy
accepts_nested_attributes_for :donors
# Donation.total :integer
validate :validate_donors_total_matches_donation_total
def validate_donors_total_matches_donation_total
# Need to figure out how to count the sum of all donors
end
end
class Donor < ActiveRecord:Base
belongs_to :donation
end