简体   繁体   中英

Both model classes having belongs_to to each other in Rails/ActiveRecord

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?

I think what you're looking for is has_and_belongs_to_many, which pecifies a many-to-many relationship with another class.

http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_and_belongs_to_many

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM