# Re: Creating a second instance of a singleton class?

**URL:** <https://www.ruby-forum.com/t/re-creating-a-second-instance-of-a-singleton-class/85603>\
**Category:** Ruby\
**Created:** [February 14, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/re-creating-a-second-instance-of-a-singleton-class/85603 "2007-02-14T21:59:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjmadsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@sjmadsen](https://www.ruby-forum.com/u/sjmadsen)\
**Post date:** [February 14, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/re-creating-a-second-instance-of-a-singleton-class/85603/1 "2007-02-14T21:59:41Z")

</div>

I love this group b/c there are so many clever people here. Ara -  
you’re definitely getting me close.

I’ll open the bag a little at the expense of brevity so you can see  
what I’m trying to accomplish. I’m using Rails v1.2.2, and in  
particular ActiveRecord 1.15.2.

In the course of a data operation, I have a series of active record  
instances:

instance1ofclass1, instance2ofclass1, instance1ofclass2,  
instance2ofclass2, etc…]

For any given activerecord class for the above list (let’s use an  
example table called “property”) there is an identical table (less the  
primary key) called “property\_versions”

I would like to be able to create a new instance of the activerecord  
model but using a new table name - changing the table\_name is handled  
as a class method “set\_table\_name(new\_name)”

So I could say:

#==\> assumes we have a “property” instance available…  
orig\_table = Property.table\_name  
class Property  
set\_table\_name ‘property\_versions’  
end

prop\_versions = Property.new(property.attributes)

class Property  
set\_table\_name orig\_table  
end

But this doesn’t seem threadsafe - I could block the whole operation  
but (at the time) it seemed cleaner and simpler to just modify the  
instance of the class rather than the whole parent class.

I may just be an idiot and there’s a way easier way to handle this. But  
this was supposed to be a quick little function. Isn’t working out like  
that though. 🙂

Thanks again for any insight,

Steve

---

<div class="post-metadata">

**Author:** ![sjmadsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@sjmadsen](https://www.ruby-forum.com/u/sjmadsen)\
**Post date:** [February 15, 2007, 3:47am UTC](https://www.ruby-forum.com/t/re-creating-a-second-instance-of-a-singleton-class/85603/2 "2007-02-15T03:47:13Z")

</div>

On Thu, 15 Feb 2007, Steve M. wrote:

> instance1ofclass1, instance2ofclass1, instance1ofclass2, instance2ofclass2,  
> So I could say:  
> set\_table\_name orig\_table  
> end

you lost me. why wouldn’t you just do

property\_versions\_class = Class.new(Property){ set\_table\_name  
‘property\_versions’ }

prop\_versions = property\_versions\_class.new(property.attributes)

??

and that’s assuming it had to be dynamic. for that matter why not

./script/generate model PropertyVersion

and

prop\_versions = PropertyVersion.new(property.attributes)

??

-a
