Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1058275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:00:08+00:00 2026-05-16T18:00:08+00:00

I have the following models: class Instance < ActiveRecord::Base has_many :users has_many :books end

  • 0

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:

  • When a book is Created, a record is
    added to the BookRevision table, so
    we know who created the book in the
    first place
  • 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?
– Add a migration for the BookRevision table….
rails generate migration AddTableBookRevision user_id:integer
version:integer diff:text
– 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:00:09+00:00Added an answer on May 16, 2026 at 6:00 pm

    You might want to check out something like acts_as_versioned instead of rolling your own. This works in the fashion you’ve described here, where modifications are saved into a separate but related table.

    Keep in mind you will have to apply migrations to your Book and BookRevision table in parallel from that point forward. They must be schema compatible for revisioning to work.

    I have built a version tracking system of this sort that used serialized models to avoid having to maintain migrations, as the intent was to preserve the exact state of the model regardless of future modifications via migrations. This has the disadvantage of not being able to roll back to an arbitrary older version because there may be a schema mis-match.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: class FieldEntryValue < ActiveRecord::Base belongs_to :field_entry end and class
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have the following models: class Match < ActiveRecord::Base has_and_belongs_to_many :teams end And class
I have two models, Page and PageContent. class Page < ActiveRecord::Base has_many :page_contents end
Given I have the following class class listing > ActiveRecord::Base attr_accessible :address belongs_to :owner
Let's say I have the following models: Class Wishlist belongs_to :user # User class
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
I have the following models: class Foo has_and_belongs_to_many :bars end class Bar has_and_belongs_to_many :foos
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
I'm having a problem testing the following model: class Bill < ActiveRecord::Base belongs_to :consignee

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.