Model validation of external references

Say I have the following situation:

class Client < ActiveRecord::Base         has_many :orders ... end

class Order < ActiveRecord::Base         belongs_to :client ... end

How can I validate that an order that is inputted at generation of a client is valid? I have a form where you can create new clients and input a comma-separated list of orders by name (named orders) . I'm trying to put something like this in the validate method of the client model: errors.add("Invalid order") unless orders.each(valid?) How should I do that?

Bart

Bart Braem wrote:

Say I have the following situation:

class Client < ActiveRecord::Base         has_many :orders ... end

class Order < ActiveRecord::Base         belongs_to :client ... end

How can I validate that an order that is inputted at generation of a client is valid? I have a form where you can create new clients and input a comma-separated list of orders by name (named orders) . I'm trying to put something like this in the validate method of the client model: errors.add("Invalid order") unless orders.each(valid?) How should I do that?

validates_associated :orders,                       :on => :create,                       :message => 'An order is invalid'

would work, but watch out for this bug:    http://dev.rubyonrails.org/ticket/4276