Creating and saving multiple instances to subclass of Active


#1

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


#2

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


#3

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.


#4

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.


#5

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


#6

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!