How about this:

class Conversation < ActiveRecord::Base
  validates_presence_of :leader
end

That way you can't save a conversation without assigning it a leader,
but the leader doesn't have to be saved yet.

On Dec 30, 3:30 pm, Evan Chaney <[email protected]> wrote:
> I have a model “Conversation” and a model “Leader”. A Conversation is always 
> led by exactly 1 Leader.
>
> I’ve overridden Conversation’s “validate” method to validate the presence of 
> an associated Leader model.
>
> If I add a similar validation to the Leader model, however, Conversations can 
> no longer be saved on creation because the Leader model is invalid.
>
> On creation of both a new Conversation and new Leader how can I validate in 
> each model that it is associated with the other? Is there a best practice? 
> The closest I’ve come is to override Leader’s “validate_on_update” method to 
> ensure the presence of a Conversation but that still allows new Leader 
> objects to be created without a Conversation, filling my tables with junk.

--

You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to