Skip to content

Instantly share code, notes, and snippets.

@jpemberthy
Created July 15, 2009 20:27
Show Gist options
  • Save jpemberthy/147959 to your computer and use it in GitHub Desktop.
Save jpemberthy/147959 to your computer and use it in GitHub Desktop.
init.rb
def has_one_with_deleted(association_id, options = {})
with_deleted = options.delete :with_deleted
returning has_one_without_deleted(association_id, options) do
if with_deleted
reflection = reflect_on_association(association_id)
association_accessor_methods(reflection, Caboose::Acts::HasOneThroughWithDeletedAssociation)
association_constructor_method(:build, reflection, Caboose::Acts::HasOneThroughWithDeletedAssociation)
association_constructor_method(:create, reflection, Caboose::Acts::HasOneThroughWithDeletedAssociation)
end
end
end
alias_method_chain :has_one, :deleted
module Caboose # :nodoc:
module Acts # :nodoc:
class HasOneThroughWithDeletedAssociation < ActiveRecord::Associations::HasOneAssociation
private
def find_target
@reflection.klass.find_with_deleted(:first,
:order => @reflection.options[:order],
:include => @reflection.options[:include]
)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment