Fabrice Fabrisss
2011-Jan-05 08:52 UTC
find_or_create : how to know the resulting operation ?
hello, find_or_create_by has a different behaviour according to the existence of the instance in the database, is it possible to know the resulting operation (find or create) which has been actually executed ? thank you, Fabrice -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tim Shaffer
2011-Jan-05 13:41 UTC
Re: find_or_create : how to know the resulting operation ?
With find_or_create, I don''t believe there is a way. You can certainly do it with find_or_initialize winter = Tag.find_or_initialize_by_name("Winter") winter.persisted? # true if record exists, false if it does not -- 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.
Just a thought. Maybe you can use a call back before the record is saved to set up a virtual attribute based on the result of new_record? On Jan 5, 3:52 am, Fabrice Fabrisss <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> hello, > > find_or_create_by has a different behaviour according to the existence > of the instance in the database, > > is it possible to know the resulting operation (find or create) which > has been actually executed ? > > thank you, > > Fabrice > > -- > Posted viahttp://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.
Marnen Laibow-Koser
2011-Jan-06 15:49 UTC
Re: find_or_create : how to know the resulting operation ?
Fabrice Fabrisss wrote in post #972422:> hello, > > find_or_create_by has a different behaviour according to the existence > of the instance in the database, > > is it possible to know the resulting operation (find or create) which > has been actually executed ?Why do you care? What''s your use case?> > thank you, > > FabriceBest, -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fabrice Fabrisss
2011-Jan-06 15:54 UTC
Re: find_or_create : how to know the resulting operation ?
Thank you for your answers, I like the solution with the ''initialize'' function but I still haven''t tried it. I have a counter which counts the number of new occurence I will insert in the database, that''s why I want to be able to distinguish both cases, Sincerely, Fabrice -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2011-Jan-06 15:56 UTC
Re: find_or_create : how to know the resulting operation ?
Please quote when replying. Fabrice Fabrisss wrote in post #972838:> Thank you for your answers, > > I like the solution with the ''initialize'' function but I still haven''t > tried it. > > I have a counter which counts the number of new occurence I will insert > in the database, that''s why I want to be able to distinguish both cases,Please explain further. I strongly suspect that there is a better way to accomplish what you''re trying to do -- perhaps having the count done on the DB side would be useful.> > Sincerely, > > FabriceBest, -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.