Forum: Ruby on Rails Creating and saving multiple instances to subclass of Active

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
gberz3 (Guest)
on 2007-04-19 03:33
(Received via mailing list)
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
gberz3 (Guest)
on 2007-04-19 15:22
(Received via mailing list)
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
Mark Reginald J. (Guest)
on 2007-04-19 16:19
(Received via mailing list)
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.
gberz3 (Guest)
on 2007-04-19 20:30
(Received via mailing list)
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
Mark Reginald J. (Guest)
on 2007-04-19 20:54
(Received via mailing list)
gberz3 wrote:

> 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.
gberz3 (Guest)
on 2007-04-20 01:10
(Received via mailing list)
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!
This topic is locked and can not be replied to.