# Creating and saving multiple instances to subclass of Active

**URL:** <https://www.ruby-forum.com/t/creating-and-saving-multiple-instances-to-subclass-of-active/92393>\
**Category:** Rails\
**Created:** [April 19, 2007, 1:33am UTC](https://www.ruby-forum.com/t/creating-and-saving-multiple-instances-to-subclass-of-active/92393 "2007-04-19T01:33:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [April 19, 2007, 1:33am UTC](https://www.ruby-forum.com/t/creating-and-saving-multiple-instances-to-subclass-of-active/92393/1 "2007-04-19T01:33:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [April 19, 2007, 1:22pm UTC](https://www.ruby-forum.com/t/creating-and-saving-multiple-instances-to-subclass-of-active/92393/2 "2007-04-19T13:22:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [April 19, 2007, 2:19pm UTC](https://www.ruby-forum.com/t/creating-and-saving-multiple-instances-to-subclass-of-active/92393/3 "2007-04-19T14:19:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [April 19, 2007, 6:54pm UTC](https://www.ruby-forum.com/t/creating-and-saving-multiple-instances-to-subclass-of-active/92393/4 "2007-04-19T18:54:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [April 19, 2007, 6:30pm UTC](https://www.ruby-forum.com/t/creating-and-saving-multiple-instances-to-subclass-of-active/92393/5 "2007-04-19T18:30:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [April 19, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/creating-and-saving-multiple-instances-to-subclass-of-active/92393/6 "2007-04-19T23:10:06Z")

</div>

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!
