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 584177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:52:14+00:00 2026-05-13T14:52:14+00:00

I’m creating a wiki. Each Article has_many Revisions, and an Article belongs_to a single

  • 0

I’m creating a wiki. Each Article has_many Revisions, and an Article belongs_to a single current_revision. So in the database, Articles have a single reference to a Revision’s id, and Revisions each have a single reference to the Article they belong to. Before I continue, does this seem like a sane way to do things? It strikes me as fairly unorthodox, but logical, and I’m not sure how others in similar situations set things up.

The trouble is that this type of mutual belongs_to relationship seems to really throw Rails off when creating the models. When I first create an Article, I’d like to also create an initial Revision to go with it.

I added a before_create method and did something like:

initial_revision = self.revisions.build
self.current_revision = initial_revision

but this would cause a stack overflow on save, as Rails apparently tries in a loop to first save the Article, so it has an article_id to stick in the Revision, and then first save the Revision, so it has a current_revision_id to stick in the Article.

When I break things up, and don’t create them simultaneously (but still in a transaction), the first one created doesn’t get its reference set.
For example:

initial_revision = Revisions.create
self.current_revision = initial_revision
initial_revision.article = self

would leave the revision with a null article_id as it missed the save.

I think I could get around this by calling an after_create method as well, just to initialize the variable with an update and save, but this is turning into a gigantic mess, and I feel like in Rails that usually means I’m doing something wrong-headedly.

Can anyone help, or am I stuck creating a little after_create method that saves in the change?

  • 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-13T14:52:14+00:00Added an answer on May 13, 2026 at 2:52 pm

    I has similar problem recently. You need to declare only one way of association. Can your Article be created without Revision, and then Revision added to existing Article?

    Or can you point from Article to Revision which is not pointing back? If that should be not possible, then you need to declare Revision as belongs_to :article, and Article :has_many :revisions and has_one :revision, :conditions => { ... }. And add flag ‘main revision’ to revision model or get last revision by date.

    This way you don’t provide cyclic dependencies, so it should be easier.

    Edit:
    This is how I tested it and make it work:

    class Article < ActiveRecord::Base
      has_many :revisions
      has_one :current_revision, :class_name => "Revision", :conditions => { :tag => "current" }
    
      before_validation do |article|
        # add current revision to list of all revisions, and mark first revision as current unless one is marked as current
        article.current_revision = article.revisions.first unless article.current_revision.present?
        article.revisions << article.current_revision if article.current_revision.present? and not article.revisions.member?(article.current_revision)
      end
    
      after_save do |article|
        article.current_revision.mark_as_current if article.current_revision.present?
      end
    end
    
    class Revision < ActiveRecord::Base
      belongs_to :article
    
      def mark_as_current
        Revision.update_all("tag = ''", :article_id => self.article_id)
        self.tag = "current"
        save!
      end
    
    end
    

    And this is how it works now (dump from script/console):

    $ ./script/console
    Loading development environment (Rails 2.3.5)
    >> a1 = Article.new :name => "A1"
    >> a1.revisions.build :number => 1
    >> a1.save
    >> a1.reload
    >> a1.revisions
    +----+------------+--------+---------+-------------------------+-------------------------+
    | id | article_id | number | tag     | created_at              | updated_at              |
    +----+------------+--------+---------+-------------------------+-------------------------+
    | 1  | 1          | 1      | current | 2010-02-03 19:10:37 UTC | 2010-02-03 19:10:37 UTC |
    +----+------------+--------+---------+-------------------------+-------------------------+
    >> a1.current_revision
    +----+------------+--------+---------+-------------------------+-------------------------+
    | id | article_id | number | tag     | created_at              | updated_at              |
    +----+------------+--------+---------+-------------------------+-------------------------+
    | 1  | 1          | 1      | current | 2010-02-03 19:10:37 UTC | 2010-02-03 19:10:37 UTC |
    +----+------------+--------+---------+-------------------------+-------------------------+
    >> a1r2 = a1.revisions.build :number => 2
    +------------+--------+-----+------------+------------+
    | article_id | number | tag | created_at | updated_at |
    +------------+--------+-----+------------+------------+
    | 1          | 2      |     |            |            |
    +------------+--------+-----+------------+------------+
    >> a1r2.mark_as_current
    >> a1.revisions
    +----+------------+--------+---------+-------------------------+-------------------------+
    | id | article_id | number | tag     | created_at              | updated_at              |
    +----+------------+--------+---------+-------------------------+-------------------------+
    | 1  | 1          | 1      | current | 2010-02-03 19:10:37 UTC | 2010-02-03 19:10:37 UTC |
    | 2  | 1          | 2      | current | 2010-02-03 19:11:44 UTC | 2010-02-03 19:11:44 UTC |
    +----+------------+--------+---------+-------------------------+-------------------------+
    >> a1.revisions.reload
    +----+------------+--------+---------+-------------------------+-------------------------+
    | id | article_id | number | tag     | created_at              | updated_at              |
    +----+------------+--------+---------+-------------------------+-------------------------+
    | 1  | 1          | 1      |         | 2010-02-03 19:10:37 UTC | 2010-02-03 19:10:37 UTC |
    | 2  | 1          | 2      | current | 2010-02-03 19:11:44 UTC | 2010-02-03 19:11:44 UTC |
    +----+------------+--------+---------+-------------------------+-------------------------+
    >> a1.current_revision
    +----+------------+--------+---------+-------------------------+-------------------------+
    | id | article_id | number | tag     | created_at              | updated_at              |
    +----+------------+--------+---------+-------------------------+-------------------------+
    | 1  | 1          | 1      | current | 2010-02-03 19:10:37 UTC | 2010-02-03 19:10:37 UTC |
    +----+------------+--------+---------+-------------------------+-------------------------+
    >> a1.reload
    >> a1.current_revision
    +----+------------+--------+---------+-------------------------+-------------------------+
    | id | article_id | number | tag     | created_at              | updated_at              |
    +----+------------+--------+---------+-------------------------+-------------------------+
    | 2  | 1          | 2      | current | 2010-02-03 19:11:44 UTC | 2010-02-03 19:11:44 UTC |
    +----+------------+--------+---------+-------------------------+-------------------------+
    

    Watch for problem with two revisions marked as current before reload revisions collection on article. When you mark one of revisions as current, then you need to reload you whole article object (if you want to use current_revision field) or only revision collection.

    And you should probably treat current_revision only as a read-only pointer. If you try to assign another revision to it, then you’ll loose previous revision which was pointed by article as current (Rails will remove old referenced object, because of has_one).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.