# Adding an associated Table to an Existing Table (Book \> Revision)

**URL:** <https://www.ruby-forum.com/t/adding-an-associated-table-to-an-existing-table-book-revision/194831>\
**Category:** Rails\
**Created:** [September 11, 2010, 7:15pm UTC](https://www.ruby-forum.com/t/adding-an-associated-table-to-an-existing-table-book-revision/194831 "2010-09-11T19:15:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 11, 2010, 7:15pm UTC](https://www.ruby-forum.com/t/adding-an-associated-table-to-an-existing-table-book-revision/194831/1 "2010-09-11T19:15:34Z")

</div>

Hello, newbie here…

I have the following models:

class Instance \< ActiveRecord::Base  
has\_many :users  
has\_many :books  
end

class User \< ActiveRecord::Base  
belongs\_to :instance  
end

class Book \< ActiveRecord::Base  
belongs\_to :instance  
end

I now want to add a BookRevision table, that has the following columns  
(id, user\_id, version # (auto increment), diff (old book copy),  
timestamps)

Logic:

1. When a book is Created, a record is added to the BookRevision  
table, so we know who created the book in the first place
2. When a book is Updated, a record is added with the user\_id (could  
be a different user), and a new version #, and the old book text, to  
serve as an archive.

Given that I have the Instance, User, Book table implement in my rails  
add, are these the correct steps to make the above come to life?

1. Add a migration for the BookRevision table…  
rails generate migration AddTableBookRevision user\_id:integer  
version:integer diff:text
2. Then update the models as follows:

class Instance \< ActiveRecord::Base  
has\_many :users  
has\_many :books  
end

class User \< ActiveRecord::Base  
belongs\_to :instance  
end

class Book \< ActiveRecord::Base  
belongs\_to :instance  
has\_many :BookRevisions  
end

class BookRevision \< ActiveRecord::Base  
belongs\_to :Book  
end

Then in my controller, when adding a new book? Right now I have:  
@book = Book.create(params[:book].merge(:instance\_id =\>  
current\_user.instance\_id))  
How do I update that to account for the BookRevision association?

Thanks for helping me out!

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 11, 2010, 7:35pm UTC](https://www.ruby-forum.com/t/adding-an-associated-table-to-an-existing-table-book-revision/194831/2 "2010-09-11T19:35:28Z")

</div>

you and to record every update or you overwrite when there is an update?

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 11, 2010, 7:37pm UTC](https://www.ruby-forum.com/t/adding-an-associated-table-to-an-existing-table-book-revision/194831/3 "2010-09-11T19:37:49Z")

</div>

I want to add a revision record on every update to the book record.

Like how a wiki works. Every time the wiki is update it tracks a  
revision:  
[http://upload.wikimedia.org/wikipedia/commons/4/41/Mediawiki-database-schema.png](http://upload.wikimedia.org/wikipedia/commons/4/41/Mediawiki-database-schema.png)

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 11, 2010, 7:41pm UTC](https://www.ruby-forum.com/t/adding-an-associated-table-to-an-existing-table-book-revision/194831/4 "2010-09-11T19:41:55Z")

</div>

see if this helps

> **[\#177 Model Versioning - RailsCasts](http://railscasts.com/episodes/177-model-versioning)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 11, 2010, 8:31pm UTC](https://www.ruby-forum.com/t/adding-an-associated-table-to-an-existing-table-book-revision/194831/5 "2010-09-11T20:31:06Z")

</div>

To install Vestal Versions, after adding the gem you have to run:  
“script/generate vestal\_versions” not sure if that’s a Rails 3 command  
so I tried: “rails generate vestal\_versions” which gave “Could not  
find generator vestal\_versions”

I did check to make sure it was installed:  
$ bundle show vestal\_versions  
/Users/me/Sites/cline/vestal\_versions/ruby/1.8/gems/  
vestal\_versions-1.0.2

Any ideas on how to install Vestal Versions? thxs again!

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 11, 2010, 8:36pm UTC](https://www.ruby-forum.com/t/adding-an-associated-table-to-an-existing-table-book-revision/194831/6 "2010-09-11T20:36:22Z")

</div>

Also tried:

rails g vestal\_versions  
Could not find generator vestal\_versions

Anyone out there able to get Vestal Versions working with Rails 3?

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 12, 2010, 4:59am UTC](https://www.ruby-forum.com/t/adding-an-associated-table-to-an-existing-table-book-revision/194831/7 "2010-09-12T04:59:42Z")

</div>

well im going to sleep now but ill check it out tomorrow

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 11, 2010, 8:04pm UTC](https://www.ruby-forum.com/t/adding-an-associated-table-to-an-existing-table-book-revision/194831/8 "2010-09-11T20:04:45Z")

</div>

Vestal Versions looks pretty impressive, only challenge is it doesn’t  
seem to play nice with Rails 3.

“ActiveRecord::DangerousAttributeError: changes is defined by  
ActiveRecord” It uses a field in the model called changes. Not sure if  
there are plans to fix that or not.
