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

  • Home
  • SEARCH
  • 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 8538731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:11:30+00:00 2026-06-11T11:11:30+00:00

I have a versioning mechanism for a Lesson model: class Lesson < ActiveRecord::Base attr_accessible

  • 0

I have a versioning mechanism for a Lesson model:

class Lesson < ActiveRecord::Base
  attr_accessible :lesson_version_id, :number
  belongs_to :lesson_version
end

class LessonVersion < ActiveRecord::Base
  attr_accessible :lesson_id, :version
  belongs_to :lesson
end

There’s a 1-many relationship between Lesson and LessonVersion. I’ve modeled that by having belongs_to :lesson in the LessonVersion class. For now, I won’t deal with the has_many :lesson_versions in the Lesson class part of that relationship.

Secondly, I also need to know the latest version for each lesson to be able to immediately pull it up. So in the Lesson model I need a reference to a LessonVersion. I guess this is a 1-1 type relationship.

Does this make sense to model it this way? Will I get in trouble having a belongs_to in each class to each other?

  • 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-06-11T11:11:31+00:00Added an answer on June 11, 2026 at 11:11 am

    Instead of creating another relationship, I would use a scope on LessonVersion.

    class Lesson < ActiveRecord::Base
      attr_accessible :number
      has_many :lesson_versions
    
      def most_recent_version
        lesson_versions.most_recent.first
      end
    end
    
    class LessonVersion < ActiveRecord::Base
      attr_accessible :lesson_id, :version
      belongs_to :lesson
      scope :recent, order("created_at DESC")
    end
    

    Also, you might prefer to order by "version DESC" if version is an integer that increases with each new version.

    EDIT: After reading your comment, I have two suggestions.

    Suggestion 1

    Use my solution above, but with "version DESC" for the sort, and when you want to revert to an older version, instead duplicate it and make it the newest version. Then you have a history of all edits and don’t need

    As for the order in the query, if you add a compound index on version and lesson_id in the database this will be a trivial operation. Also, calling #first on a scope is efficient, it will query with LIMIT 1 and only instantiate the record you care about. And the result of the query is automatically cached if you need to use this method multiple times in the same request.

    Suggestion 2

    Set up a belongs_to on Lesson, but name it :current_version so as to prevent confusion.

    class Lesson < ActiveRecord::Base
      attr_accessible :number, :current_version_id
      has_many :lesson_versions
      belongs_to :current_version, :class_name => "LessonVersion"
    end
    
    class LessonVersion < ActiveRecord::Base
      attr_accessible :lesson_id, :version
      belongs_to :lesson
      scope :recent, order("created_at DESC")
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom routing class that checks versioning of an object to allow
I have an existing iPad application to which I've just added core data versioning.
I have a webapp to be ran on Tomcat6/JbossAS 5 and, for versioning purposes,
have written this little class, which generates a UUID every time an object of
I have a versioning issue with a WCF service contract in which one of
i am wondering how can i have simple versioning, say i want to be
I have entities that required versioning support and from time to time, i will
I have worked on an Android project for which I've used versioning locally and
I have a project hosted on GitHub and I use Git for source versioning.
We have the convention of versioning our builds as [major].[minor].[micro].[revision], e.g. 2.1.2.33546. Our build-script

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.