Skip to content

Instantly share code, notes, and snippets.

@Holek
Created March 13, 2014 15:16
Show Gist options
  • Save Holek/9530383 to your computer and use it in GitHub Desktop.
Save Holek/9530383 to your computer and use it in GitHub Desktop.
Ruby on Rails/ActiveRecord 3.2.17 monkey patch for lock requests

This monkey patch allows you to use lock hints defined by AR-JDBC adapter for MSSQL with Rails 3.2.

It will not work with Rails 3.1 or 4. It might as well not work with anything else than MSSQL.

Not like it has to...

It will work with this fork, though.

# -*- encoding : utf-8 -*-
require 'active_record/associations/builder/association'
require 'active_record/associations/association_scope'
module ActiveRecord
module Associations
class AssociationScope #:nodoc:
def scope
scope = klass.unscoped
scope = scope.extending(*Array.wrap(options[:extend]))
# 13/03/2014 14:01 MP: we're adding an optional lock hint
# for mssql to pick up while searching for a record.
scope = scope.lock(options[:lock_hint]) if options[:lock_hint].present?
# It's okay to just apply all these like this. The options will only be present if the
# association supports that option; this is enforced by the association builder.
scope = scope.apply_finder_options(options.slice(
:readonly, :include, :order, :limit, :joins, :group, :having, :offset, :select))
if options[:through] && !options[:include]
scope = scope.includes(source_options[:include])
end
scope = scope.uniq if options[:uniq]
add_constraints(scope)
end
end
end
end
association_class = ActiveRecord::Associations::Builder::Association
association_class.descendants.each do |klass|
klass.instance_eval { self.valid_options += [:lock_hint] }
end
association_class.instance_eval { self.valid_options += [:lock_hint] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment