7

相关代码: http: //pastebin.com/EnLJUJ8G

class Task < ActiveRecord::Base
  after_create :check_room_schedule

  ...

  scope :for_date, lambda { |date| where(day: date) }
  scope :for_room, lambda { |room| where(room: room) }

  scope :room_stats, lambda { |room| where(room: room) }
  scope :gear_stats, lambda { |gear| where(gear: gear) } 

  def check_room_schedule
    @tasks = Task.for_date(self.day).for_room(self.room).list_in_asc_order
    @self_position = @tasks.index(self)

    if @tasks.length <= 2
      if @self_position == 0 
        self.notes = "There is another meeting in 
    this room beginning at # {@tasks[1].begin.strftime("%I:%M%P")}."
        self.save
      end
    end
  end

  private

    def self.list_in_asc_order
      order('begin asc')
    end
end

我正在制作一个小任务应用程序。每个任务都分配到一个房间。添加任务后,我想使用回调来检查在我刚刚添加的任务之前和之后在同一个房间中是否有任务(尽管我的代码现在只处理一个边缘情况)。

所以我决定使用 after_create (因为如果他们编辑它,用户将手动检查它,因此不是 after_save)所以我可以使用两个范围和一个类方法来查询当天的任务,在房间里,并订购它们时间。然后我在数组中找到对象并开始使用 if 语句。

我必须明确保存对象。有用。但是我这样做感觉很奇怪。我不是太有经验(第一个应用程序),所以我不确定这是不赞成还是约定俗成。我搜索了一堆并翻阅了一本参考书,但我没有看到任何具体的内容。

谢谢。

4

1 回答 1

3

before_create这对我来说似乎是一项任务。如果您必须保存after_*回调,您可能打算使用before_*回调。

before_create您不必调用save,因为保存发生在回调代码为您运行之后。

与其保存然后查询是否获得 2 个或更多对象返回,不如在保存之前查询一个会发生冲突的对象。

在伪代码中,您现在拥有的内容:

after creation
  now that I'm saved, find all tasks in my room and at my time
  did I find more than one?
    Am I the first one?
      yes: add note about another task, then save again
      no: everything is fine, no need to re-save any edits

你应该拥有的:

before creation
  is there at least 1 task in this room at the same time?
    yes: add note about another task
    no: everything is fine, allow saving without modification

更像这样的东西:

before_create :check_room_schedule
def check_room_schedule
  conflicting_task = Task.for_date(self.day)
                         .for_room(self.room)
                         .where(begin: self.begin) # unsure what logic you need here...
                         .first
  if conflicting_task
    self.notes =
      "There is another meeting in this room beginning at #{conflicting_task.begin.strftime("%I:%M%P")}."
  end
end
于 2012-09-24T19:24:04.910 回答