Skip to content

Instantly share code, notes, and snippets.

@radar
Created April 2, 2010 21:25
Show Gist options
  • Save radar/353732 to your computer and use it in GitHub Desktop.
Save radar/353732 to your computer and use it in GitHub Desktop.
#failing with undefined method `association_join' for class
#`ActiveRecord::Associations::ClassMethods::JoinAssociation'
/*
Here's a patch to rails that fixes it's failure to quote a table name in one branch
of this monster case statement.
Meanwhile - i'm trying to get this monkeypatch working.
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -2163,10 +2162,10 @@ module ActiveRecord
foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
" #{join_type} %s ON %s.%s = %s.%s " % [
table_name_and_alias,
- aliased_table_name,
- foreign_key,
- parent.aliased_table_name,
- reflection.options[:primary_key] || parent.primary_key
+ connection.quote_table_name(aliased_table_name),
+ connection.quote_column_name(foreign_key),
+ connection.quote_table_name(parent.aliased_table_name),
+ reflection.options[:primary_key] || parent.primary_key
]
end
when :belongs_to
*/
require 'active_record'
require 'active_record/Associations.rb'
class ActiveRecord::Associations::ClassMethods::JoinDependency
class JoinAssociation < JoinBase
if Rails::VERSION::STRING == '2.3.5'
alias_method :original_assocation_join, :association_join
def association_join
join = ''
puts 'HERE'
connection = reflection.active_record.connection
if ([:has_many, :has_one].include?(reflection.macro) &&
! reflection.options[:through] &&
! reflection.options[:as] )
foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
join = " #{join_type} %s ON %s.%s = %s.%s " % [
table_name_and_alias,
connection.quote_table_name(aliased_table_name),
connection.quote_column_name(foreign_key),
connection.quote_table_name(parent.aliased_table_name),
reflection.options[:primary_key] || parent.primary_key
]
join << %(AND %s) % [
klass.send(:type_condition, aliased_table_name)] unless klass.descends_from_active_record?
[through_reflection, reflection].each do |ref|
join << "AND #{interpolate_sql(sanitize_sql(ref.options[:conditions], aliased_table_name))} " if ref && ref.options[:conditions]
end
puts "overrode join to add quotes : #{join}"
else
join = original_assocation_join()
end
join
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment