has_one_paranoid

I ran in to a problem with AR::B#find(:include) and acts_as_paranoid, well described on the ruby mailing list.

So I whipped up this thing, which seems to work for me:

1
2
3
4
5
6
7
8
9
10
11
12
module ActiveRecord
 class Base
   def self.has_one_paranoid(*args)
     ref = create_has_one_reflection *args
     cond = args.last[:conditions]
     cond = cond.blank? ? '' : cond + ' AND'
     cond << " (#{ref.table_name}.deleted_at IS NULL OR #{ref.table_name}.deleted_at > now())"
     args.last[:conditions] = cond
     has_one *args
   end
 end
end

Leave a Reply