给定一个有default_scope个可过滤所有过期条目的模型:
# == Schema Information
#
# id :integer(4) not null, primary key
# user_id :integer(4) not null, primary key
# end_date :datetime
class Ticket < ActiveRecord::Base
belongs_to :user
default_scope :conditions => "tickets.end_date > NOW()"
end
现在我想买any张票.在这种情况下,with_exclusive_scope是一条出路,但这种方法是否受到保护?只有这样才能奏效:
Ticket.send(:with_exclusive_scope) { find(:all) }
有点像黑客,不是吗?那么正确的使用方法是什么呢?尤其是在处理关联时,情况变得更糟(考虑到一个用户有很多罚单):
Ticket.send(:with_exclusive_scope) { user.tickets.find(:all) }
那是so个丑陋的人!!!-不可能是铁路吗!?