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?
Instead of creating another relationship, I would use a scope on
LessonVersion.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 needAs for the order in the query, if you add a compound index on
versionandlesson_idin the database this will be a trivial operation. Also, calling#firston a scope is efficient, it will query withLIMIT 1and 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.