【发布时间】:2011-08-14 21:03:48
【问题描述】:
我有 3 个模型
class Task < ActiveRecord::Base
belongs_to :user, :dependent => :destroy
has_many :clock_ins
accepts_nested_attributes_for :clock_ins, :allow_destroy => true
end
class ClockIn < ActiveRecord::Base
belongs_to :task, :dependent => :destroy
has_one :clock_out
end
class ClockOut < ActiveRecord::Base
belongs_to :clock_in, :dependent => :destroy
end
目前我可以为每个Task 创建一个ClockIn。
当我开始一个新的ClockIn 时,我想为当前打开的Task 创建一个ClockOut。
如何在我的任务中搜索具有ClockIn 而没有ClockOut 的任务?
解决方案
组合模型
修复破坏
迭代所有任务然后更新
task.clocks.where(:clock_out => nil).first.update_attribute :clock_out, Time.now
【问题讨论】:
-
请检查“查找所有没有关联的记录”问题。
-
很接近,但由于我正在深入挖掘一层关系,所以这不是正确的答案。
标签: ruby-on-rails-3 activerecord model controller