简体   繁体   English

两个模型类在Rails / ActiveRecord中具有相互之间的归属

[英]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 . LessonLessonVersion之间存在一对多的关系。 I've modeled that by having belongs_to :lesson in the LessonVersion class. 我已经通过在LessonVersion类中拥有belongs_to :lessonLessonVersion 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. 至于查询的顺序,如果您在数据库中的versionlesson_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.

相关问题 Rails FactoryGirl属于属于其他2个模型的模型 - Rails FactoryGirl for model that belongs_to 2 other models Rails属于双向 - Rails belongs_to to both ways Rails:在一个属于其他两个模型的模型中创建一个新条目 - Rails: Create a new entry in a model that belongs_to two other models Rails 3在每条记录中专门使用belongs_to不同的表 - Rails 3 model with belongs_to different tables exclusively in each record Rails:模型的新条目,属于另外两个 - Rails: New entry to a model that belongs_to two other Rails-一个模型属于其他模型的单一参考 - Rails - a model belongs_to other models single reference Rails:如何在两端验证彼此是否存在的情况下,通过belongs_to + has_many关系创建新记录? - Rails: How to create a new record in a belongs_to + has_many relationship with both ends validating the presence of each other? Rails:如何使用FactoryGirl创建一个Factory,以实现一个Ares_to + has_many关系,并通过两端验证彼此的存在? - Rails: How to create a Factory using FactoryGirl for a belongs_to + has_many relationship with both ends validating the presence of each other? Rails 的 Activerecord 上的belongs_to 创建了大量查询 - belongs_to on Activerecord of Rails creates lots of queries 具有相同模型的两个belongs_to的ActiveRecord - ActiveRecord with two belongs_to of the same model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM