Evan Chaney
2009-Dec-30 20:30 UTC
Validating the presence of two associated objects from both models
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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David
2009-Dec-30 22:37 UTC
Re: Validating the presence of two associated objects from both models
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 <evancha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Evan Chaney
2009-Dec-30 22:50 UTC
Re: Validating the presence of two associated objects from both models
> That way you can''t save a conversation without assigning it a leader, > but the leader doesn''t have to be saved yet.Well, that’s where I’m at right now. I’d like to be in a situation where Conversation validates that it has a Leader and Leader validates it has a Conversation before either is saved. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rodrigo Dellacqua
2009-Dec-30 23:25 UTC
Re: Re: Validating the presence of two associated objects from both models
Evan, Don''t overdo your requirements. Take your requirements lightly. From what I can see in your example, Leader plays a larger role in your domain, so it has priority over the Conversation. I would model Leader having zero or more (0..*) Conversations, Conversation having 1 Leader. A simple one to many relationship between those domain objects. As from what I can tell, your are trying to create both at the same time in the view, thus sending both objects over to the controller. While creating a Conversation, you could give the user the opportunity to create a Leader, if its currently not available, or he wants to get a new one. You could use AJAX to access the create action in the leader controller having it available prior to saving the Conversation. Check out this resource by Ryan Bates. http://railscasts.com/episodes/57-create-model-through-text-field Regards, Rodrigo Dellacqua IBM - IGF Project Manager "Communication is the key" On Wed, Dec 30, 2009 at 8:50 PM, Evan Chaney <evanchaney-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > That way you can''t save a conversation without assigning it a leader, > > but the leader doesn''t have to be saved yet. > > Well, that’s where I’m at right now. I’d like to be in a situation where > Conversation validates that it has a Leader and Leader validates it has a > Conversation before either is saved. > > -- > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2009-Dec-31 05:41 UTC
Re: Validating the presence of two associated objects from
Evan Chaney wrote:>> That way you can''t save a conversation without assigning it a leader, >> but the leader doesn''t have to be saved yet. > > Well, that�s where I�m at right now. I�d like to be in a situation where > Conversation validates that it has a Leader and Leader validates it has > a Conversation before either is saved. >Just validate that there are keys: Conversation validates_presence_of :leader, Leader validates_presence_of :conversation (perhaps). Do the rest with foreign key constraints in the DB. As you''ve discovered, the application layer is the wrong place for this sort of simple integrity check.> -- > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en.Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Seemingly Similar Threads
- validating both sides of a has_one relationship breaks pickle/machinist tests
- prevent orphan records
- Validating Existing Models
- tips on how to write a controller test for models associated with currently logged in user
- Validating 2 related models at once not working