AR validation question

7 views
Skip to first unread message

Javix

unread,
Oct 12, 2012, 5:15:55 PM10/12/12
to rubyonra...@googlegroups.com
What is the best way to implement a validation for a withdrawal operation? So there is an Operation and Client models.
Client has_many :operations. In case of withdrawal operation, I'd like to check if the requested sum is not greater than a Client account balance. How to link the validation in the Operation model to a specific client ?

class Client < AR
  has_many :operations
end

class Operation < AR
  belongs_to :client
  attr_accessible :sum
end

The routes are defined so that an operation could only created in the context of a Client.

Thanks

Colin Law

unread,
Oct 12, 2012, 5:21:38 PM10/12/12
to rubyonra...@googlegroups.com
On 12 October 2012 22:15, Javix <s.ca...@gmail.com> wrote:
> What is the best way to implement a validation for a withdrawal operation?
> So there is an Operation and Client models.
> Client has_many :operations. In case of withdrawal operation, I'd like to
> check if the requested sum is not greater than a Client account balance. How
> to link the validation in the Operation model to a specific client ?

In the custom validation of the operation you can use self.client to
give you its client.

Colin
Reply all
Reply to author
Forward
0 new messages