# Vestal Versions Related Records

**URL:** <https://www.ruby-forum.com/t/vestal-versions-related-records/186608>\
**Category:** Rails\
**Created:** [April 9, 2010, 4:32pm UTC](https://www.ruby-forum.com/t/vestal-versions-related-records/186608 "2010-04-09T16:32:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@mikej](https://www.ruby-forum.com/u/mikej)\
**Post date:** [April 9, 2010, 4:32pm UTC](https://www.ruby-forum.com/t/vestal-versions-related-records/186608/1 "2010-04-09T16:32:58Z")

</div>

I need to keep version of a model Client:

has\_many :product\_clients  
has\_many :products, :through =\> :product\_clients

The question is what is the best way of saving versions of the  
association. Can I make the association using the client.version.id  
rather than the client.id? Or should I use a bit mask?

Any thoughts much appreciated,

Mike

---

<div class="post-metadata">

**Author:** ![mikej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@mikej](https://www.ruby-forum.com/u/mikej)\
**Post date:** [April 9, 2010, 6:39pm UTC](https://www.ruby-forum.com/t/vestal-versions-related-records/186608/2 "2010-04-09T18:39:25Z")

</div>

mikej wrote:

> I need to keep version of a model Client:
> 
> has\_many :product\_clients  
> has\_many :products, :through =\> :product\_clients
> 
> The question is what is the best way of saving versions of the  
> association. Can I make the association using the client.version.id  
> rather than the client.id? Or should I use a bit mask?
> 
> Any thoughts much appreciated,
> 
> Mike

What about adding a datetime start and a datetime end to the  
product\_clients model - expand it out to a full join table with  
additional attributes? Don’t delete the relationship, just end it. Then  
you could view which products a client had on any given date, today or  
in the past.

---

<div class="post-metadata">

**Author:** ![mikej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@mikej](https://www.ruby-forum.com/u/mikej)\
**Post date:** [April 9, 2010, 8:18pm UTC](https://www.ruby-forum.com/t/vestal-versions-related-records/186608/3 "2010-04-09T20:18:31Z")

</div>

That would work. I like the idea of making my clients serial  
monogamists.

However, it is a bit of a faff having to pass a date every time, or  
perhaps I could write this into the has\_many relationship. Am I  
missing something?

I’ve just added a version\_id (populated with the version number) to  
the product\_clients and added to the association:

has\_many :product\_clients :dependent =\> :destroy, :conditions =\>  
‘product\_clients.version\_id = #{self.send(“version”)}’  
has\_many :products, :through =\> :product\_clients

Does this seem sensible?

Mike
