Evan
2008-Oct-20 03:35 UTC
Ensuring both objects in a one-to-many association are not saved without the presence of the other
Hello. I have an object which is related to a second object through has_many. The second object (obviously) is related to the first through belongs_to. I want to ensure that neither object is saved to the database without the presence of the other. How can I do this? Thanks. Evan --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thani Ararsu
2008-Oct-20 04:45 UTC
Re: Ensuring both objects in a one-to-many association are not saved without the presence of the ot
Evan wrote:> Hello. > > I have an object which is related to a second object through has_many. > The second object (obviously) is related to the first through > belongs_to. I want to ensure that neither object is saved to the > database without the presence of the other. How can I do this? > > Thanks. > > EvanUse transaction -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Evan
2008-Oct-20 05:01 UTC
Re: Ensuring both objects in a one-to-many association are not saved without the presence of the ot
Even with transactions can I save the object containing the foreign key without saving the primary object first? In other words, can I save the object that requires the foreign key to be present if I don''t have the foreign key because the primary object has not yet been saved, thus generating an id for it? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Evan
2008-Oct-20 05:02 UTC
Re: Ensuring both objects in a one-to-many association are not saved without the presence of the ot
Oh wait. I think I just got it. Start a transaction, save the primary object thus getting the id, save the secondary object, end transaction. Right? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Evan
2008-Oct-20 05:44 UTC
Re: Ensuring both objects in a one-to-many association are not saved without the presence of the ot
I guess the question I was really getting at is how can I validate the presence and association of both objects before they are saved? I feel more confident when there is code in the model itself to ensure the integrity of the data rather than depending on each programmer to place every save in a transaction block. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---