确定是否在 Rails 回调中添加/删除了子项

发布于 2024-10-10 21:36:27 字数 834 浏览 1 评论 0原文

场景:

我有一个 habtm 关系,想要确定是否已添加或删除某个方向的子项。我正在尝试使用回调,但发现我没有对孩子进行更改的记录。有类似 course.students.changed 的​​东西吗?


使用:

  • Rails 3.0.3
  • Ruby 1.9.2p0

表:

学生 - id、first_name、last_name
课程 - ID、名称、地点
course_students - course_id、student_id


模型:

class Course
  # Callbacks
  before_save :student_maintenance

  # Relationships
  has_and_belongs_to_many :students

  protected
  def student_maintenance
    # I want to do something like
    students.changed?
      students.changes.each do |student|
        if marked_for_deletion
          # do something
        elsif marked_for_addition
          # do something else
        end
      end
    end
  end
end

class Student
  # Relationships
  has_and_belongs_to_many :courses
end

Scenario:

I have a habtm relationship and would like to determine if children in one direction have been added or deleted. I'm trying to use callbacks but find i don't have a record of changes to the children. Is there something like course.students.changed?


Using:

  • Rails 3.0.3
  • Ruby 1.9.2p0

Tables:

students - id, first_name, last_name
courses - id, name, location
courses_students - course_id, student_id


Models:

class Course
  # Callbacks
  before_save :student_maintenance

  # Relationships
  has_and_belongs_to_many :students

  protected
  def student_maintenance
    # I want to do something like
    students.changed?
      students.changes.each do |student|
        if marked_for_deletion
          # do something
        elsif marked_for_addition
          # do something else
        end
      end
    end
  end
end

class Student
  # Relationships
  has_and_belongs_to_many :courses
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声无音无过去 2024-10-17 21:36:27

如果您想捕获学生在课程中添加或删除的时间,为什么不使用班级作为连接表 *courses_students* 呢?由于这是实际创建或销毁条目的地方,因此您可以轻松地在其中使用 after_create/after_destroy 回调。

If you want to capture when a student has been added or removed from a course, why not use a class for the join table *courses_students*? Since that's the place where an entry will be actually created or destroyed you could easily use after_create/after_destroy callbacks in there.

轮廓§ 2024-10-17 21:36:27

与 Polarblau 的答案类似 - 但我希望有更多的解释:

在文本、UML 术语中,关系可能是这样的:

Course ------*-> Student

阅读:一门课程有 0 到许多学生

您可以随意在协会中添加和删除学生。

但是,您有兴趣了解有关关联的更多信息,即学生何时添加到课程或从课程中删除。有关关联的这种“额外”信息会导致您发现您需要另一个类,即“关联类”。

因此,现在您可以像这样在现有课程之间插入该课程,并随意将其称为“注册”:

Course ------*->Registration------1->Student

阅读:课程有 0 到多个注册。每次注册必须有 1 名学生。

Registration 类可能如下所示:

+--------------+
| Registration |
+--------------|
| student      |
| time_added   |
| time_dropped |
+--------------+

因此,您可以轻松获取课程当前学生的列表(其中 time_dropped 为零)。或退学学生列表(其中 time_dropped 不为零)。

Similar to polarblau's answer -- but a tad more explanation, I hope:

In textual, UML terms, the relationship might be this:

Course ------*-> Student

Read: A Course has 0 to Many Students

You can add and remove students from the association at will.

However, you are interested in knowing more about the association, that is, when the student was added or dropped from the course. This "extra" information about an association leads to discovering you need another class, a.k.a. "Association Class."

So now you would insert that class in between your existing classes as so, taking the liberty to call it a "Registration":

Course ------*->Registration------1->Student

Read: A Course has 0 to Many Registration. Each Registration must have exactly 1 Student.

The Registration class might look like this:

+--------------+
| Registration |
+--------------|
| student      |
| time_added   |
| time_dropped |
+--------------+

So you can easily get a list of current students for a course (where time_dropped is nil). Or a list of dropped students (where time_dropped is not nil).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文