Account < AR
has_many :deposits
accepts_nested_attributes_for :deposits
attr_accessible :max_amount
end
Deposit < AR
belongs_to :account
attr_accessible :amount
validate :validates_amount_less_than_max_amount
def validates_amount_less_than_max_amount
# How do you write this method? When an Account is being created with a nested
# Deposit, it should do this validation, but account is nil until
# saved, so @contribution can't access the :max_amount and validate from it.
# Solution?
end
end
views:
66answers:
2
A:
Use this validation, as expected:
def validates_amount_less_than_max_amount
errors.add(:amount, 'is more than max amount') if self.amount > account.max_amount
end
But you can't use new
to create the Account and Deposit at the same time, as you noted above. Try wrapping create Account/Deposits in a transaction:
>> Account.transaction do
>> a = Account.create!({:max_amount => 1000})
>> a.deposits_attributes = [{:amount => 1500}]
>> a.save!
>> end
ActiveRecord::RecordInvalid: Validation failed: Deposits amount is more than max amount
See what's new in edge rails 2.3 for more examples.
Jonathan Julian
2009-11-07 16:02:56
Yes, this will work but isn't there a way to move this into the models?
Gavin
2009-11-07 20:42:17
Absolutely. That's just an irb session; move it into `Account.create_with_deposit!(attributes)`.
Jonathan Julian
2009-11-08 15:50:06
A:
Here's the answer:
The patch is scheduled for 2.3.5. If you want the functionality now, you'll have to apply it and its dependencies.
Gavin
2009-11-08 00:48:35