gberz3
2007-Apr-18 23:31 UTC
Creating and saving multiple instances to subclass of ActiveRecord in a single control loop. . .?
Perhaps I''m missing something, but. . .Is it not possible to create and save multiple instances of an ActiveRecord subclass in a single control loop? For instance: require ''net/pop'' require ''md5'' def check_mail #DO SOME STUFF WITH NET::POP3 AND A POP3 SERVER mail_object = #RETURN MAIL OBJECT mail_object.each_mail do |m| proprietary_object = ProprietaryObject.new() #subclass of ActiveRecord proprietary_object.body = mail_object.body proprietary_object.to = mail_object.to proprietary_object.from = mail_object.from proprietary_object.save #FIRST SAVE WORKS. . .NO SUBSEQUENT SAVES ARE SUCCESSFUL end end . . .I have log output and it''s basically showing that the first time the save is encountered it performs an INSERT, however subsequent access performs SELECTs. This, despite the fact that I instantiate a new ProprietaryObject each time. Why is it not creating a new ProprietaryObject each go-round? Thanks, Michael --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
gberz3
2007-Apr-19 11:20 UTC
Re: Creating and saving multiple instances to subclass of ActiveRecord in a single control loop. . .?
I''ve looked through the books and I see nothing indicating that this isn''t possible. Yet this problem exists. Perhaps there''s a problem in my code? Any ideas? Michael On Apr 18, 7:31 pm, gberz3 <gbe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Perhaps I''m missing something, but. . .Is it not possible to create > and save multiple instances of an ActiveRecord subclass in a single > control loop? For instance: > > require ''net/pop'' > require ''md5'' > > def check_mail > > #DO SOME STUFF WITH NET::POP3 AND A POP3 SERVER > > mail_object = #RETURN MAIL OBJECT > > mail_object.each_mail do |m| > proprietary_object = ProprietaryObject.new() #subclass of > ActiveRecord > > proprietary_object.body = mail_object.body > proprietary_object.to = mail_object.to > proprietary_object.from = mail_object.from > > proprietary_object.save #FIRST SAVE WORKS. . .NO SUBSEQUENT SAVES > ARE SUCCESSFUL > end > end > > . . .I have log output and it''s basically showing that the first time > the save is encountered it performs an INSERT, however subsequent > access performs SELECTs. This, despite the fact that I instantiate a > new ProprietaryObject each time. Why is it not creating a new > ProprietaryObject each go-round? > > Thanks, > Michael--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2007-Apr-19 12:17 UTC
Re: Creating and saving multiple instances to subclass of ActiveRecord in a single control loop. . .?
gberz3 wrote:> I''ve looked through the books and I see nothing indicating that this > isn''t possible. Yet this problem exists. Perhaps there''s a problem > in my code? Any ideas?My guess: Because you''re wrongly writing mail_object.body, etc. instead of m.body, etc., all objects created will be identical, and you have a validates_uniqueness_of condition that is preventing objects other than the first being saved. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
gberz3
2007-Apr-19 16:29 UTC
Re: Creating and saving multiple instances to subclass of ActiveRecord in a single control loop. . .?
I''m sorry, that was just some quick pseudo code I chucked together from the original. That''s actually not the case. I''ve tried using simple loops such as: 5.times do |i| object = ProprietaryObject.new object.value = i object.save end . . .and the same issue occurs. It''s like ActiveRecord hiccups and won''t allow more than one creation in a single method call. Is the ActiveRecord stack (for lack of the appropriate word) left in an inconsistent state after saves that doesn''t allow for the creation of a new one in the same function? I''m really confused as to what is occurring. Thanks On Apr 19, 8:17 am, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> gberz3 wrote: > > I''ve looked through the books and I see nothing indicating that this > > isn''t possible. Yet this problem exists. Perhaps there''s a problem > > in my code? Any ideas? > > My guess: Because you''re wrongly writing mail_object.body, etc. > instead of m.body, etc., all objects created will be identical, > and you have a validates_uniqueness_of condition that is preventing > objects other than the first being saved. > > -- > We develop, watch us RoR, in numbers too big to ignore.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2007-Apr-19 16:45 UTC
Re: Creating and saving multiple instances to subclass of ActiveRecord in a single control loop. . .?
gberz3 wrote:> 5.times do |i| > object = ProprietaryObject.new > object.value = i > object.save > end > > > . . .and the same issue occurs. It''s like ActiveRecord hiccups and > won''t allow more than one creation in a single method call. Is the > ActiveRecord stack (for lack of the appropriate word) left in an > inconsistent state after saves that doesn''t allow for the creation of > a new one in the same function? I''m really confused as to what is > occurring.It should work fine. Have you overridden the "save" function? And try using object.save! to see if there''s any exception. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
gberz3
2007-Apr-19 21:09 UTC
Re: Creating and saving multiple instances to subclass of ActiveRecord in a single control loop. . .?
Would it be possible to begin by apologizing? Eeeeesh! The culprit was an obscure validation preventing the save from occurring for that particular subclass of ActiveRecord only. I''m not sure how Unit Testing might have fixed this particular case, but I''m certainly going to look into it more fully now. Thanks! On Apr 19, 12:45 pm, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> gberz3 wrote: > > 5.times do |i| > > object = ProprietaryObject.new > > object.value = i > > object.save > > end > > > . . .and the same issue occurs. It''s like ActiveRecord hiccups and > > won''t allow more than one creation in a single method call. Is the > > ActiveRecord stack (for lack of the appropriate word) left in an > > inconsistent state after saves that doesn''t allow for the creation of > > a new one in the same function? I''m really confused as to what is > > occurring. > > It should work fine. Have you overridden the "save" function? > And try using object.save! to see if there''s any exception. > > -- > We develop, watch us RoR, in numbers too big to ignore.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---