[英]Both model classes having belongs_to to each other in Rails/ActiveRecord
I have a versioning mechanism for a Lesson model: 我有一个用于Lesson模型的版本控制机制:
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
. Lesson
和LessonVersion
之间存在一对多的关系。 I've modeled that by having belongs_to :lesson
in the LessonVersion
class. 我已经通过在LessonVersion
类中拥有belongs_to :lesson
来LessonVersion
。 For now, I won't deal with the has_many :lesson_versions
in the Lesson class part of that relationship. 现在,我将不处理该关系的Lesson类中的has_many :lesson_versions
。
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
. 因此,在Lesson
模型中,我需要引用LessonVersion
。 I guess this is a 1-1 type relationship. 我猜这是1-1型关系。
Does this make sense to model it this way? 这样进行建模是否有意义? Will I get in trouble having a belongs_to
in each class to each other? 我会在每个类中彼此拥有一个belongs_to
遇到麻烦吗?
I think what you're looking for is has_and_belongs_to_many, which pecifies a many-to-many relationship with another class. 我认为您正在寻找的是has_and_belongs_to_many,它表示与另一个类的多对多关系。
http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_and_belongs_to_many http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_and_belongs_to_many
Instead of creating another relationship, I would use a scope on LessonVersion
. 而不是创建另一个关系,我将在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. 此外,如果version是随每个新版本增加的整数,则您可能更喜欢按"version DESC"
进行排序。
EDIT : After reading your comment, I have two suggestions. 编辑 :阅读您的评论后,我有两个建议。
Suggestion 1 建议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. 使用我上面的解决方案,但使用"version DESC"
进行排序,并且当您要还原到较旧的版本时,请复制它并使其成为最新的版本。 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. 至于查询的顺序,如果您在数据库中的version
和lesson_id
上添加复合索引,这将是微不足道的操作。 Also, calling #first
on a scope is efficient, it will query with LIMIT 1
and only instantiate the record you care about. 同样,在范围上调用#first
是有效的,它将以LIMIT 1
查询,并且仅实例化您关心的记录。 And the result of the query is automatically cached if you need to use this method multiple times in the same request. 如果您需要在同一请求中多次使用此方法,则查询结果将自动缓存。
Suggestion 2 建议2
Set up a belongs_to on Lesson, but name it :current_version so as to prevent confusion. 在Lesson上设置一个Emirates_to,但将其命名为:current_version,以免造成混淆。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.