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

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!

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

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

see if this helps
http://railscasts.com/episodes/177-model-versioning

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!

Also tried:

rails g vestal_versions
Could not find generator vestal_versions

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

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs