【问题标题】:Organize lecture with chapter and lesson models使用章节和课程模型组织讲座
【发布时间】:2015-09-06 09:16:00
【问题描述】:

我正在尝试组织我的模型,以便在 Lecture.rb > chapter.rb > course.rb 中看到。正如你所想象的,chapter.rb 必须按视图中的顺序以及章节中的嵌套课程进行组织。

这有点令人困惑。到目前为止,我的想法是创建这些模型。

class Lecture
has_many :lessons, through: :chapters
has_many :chapters
end

class Chapter
# lecture_id / name / should I use an integer to order it ? 
has_many :lessons 
belongs_to :lecture
end

class Lesson 
# I added a :step which is an integer in order to order them?
belongs_to :lecture
belongs_to :chapter
end

有没有更好的方法呢?主要目的是组织好讲座。

【问题讨论】:

  • 这和activeadmin有什么关系?
  • 我正在使用活动管理员来创建所有讲座。基本上我应该能够使用嵌套表单直接从活动管理面板创建所有章节和课程
  • 还有什么问题?如何组织协会或什么?
  • 问题是关于我的模型。我应该使用整数来组织章节和课程
  • 否则你会怎么做?

标签: ruby-on-rails ruby-on-rails-4 activeadmin


【解决方案1】:

您可能需要一个 lambda 范围块。试试这个:

class Lecture < ActiveRecord::Base
  has_many :chapters, -> { order(number: :asc) }
  has_many :lessons, through: :chapters
end

class Chapter < ActiveRecord::Base
  belongs_to :lecture
  has_many :lessons
end

class Lesson < ActiveRecord::Base
  belongs_to :chapter
  has_one :lecture, through: :chapter
end

从Deprecated warning for Rails 4 has_many with order得到它

这适用于章节表中的整数列“数字”。如果他们有订单,你可以对课程做同样的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    相关资源
    最近更新 更多