Guest User

Untitled

a guest
Apr 26th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
  2. index f0bad6c..0e6f2c4 100755
  3. --- a/activerecord/lib/active_record/associations.rb
  4. +++ b/activerecord/lib/active_record/associations.rb
  5. @@ -2228,11 +2228,13 @@ module ActiveRecord
  6.  
  7. def relation
  8. if reflection.macro == :has_and_belongs_to_many
  9. - [Arel::Table.new(table_alias_for(options[:join_table], aliased_join_table_name)), Arel::Table.new(table_name_and_alias)]
  10. + [Arel::Table.new(table_alias_for(options[:join_table], aliased_join_table_name), reflection.klass.arel_engine),
  11. + Arel::Table.new(table_name_and_alias, reflection.klass.arel_engine)]
  12. elsif reflection.options[:through]
  13. - [Arel::Table.new(table_alias_for(through_reflection.klass.table_name, aliased_join_table_name)), Arel::Table.new(table_name_and_alias)]
  14. + [Arel::Table.new(table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), through_reflection.klass.arel_engine),
  15. + Arel::Table.new(table_name_and_alias, reflection.klass.arel_engine)]
  16. else
  17. - Arel::Table.new(table_name_and_alias)
  18. + Arel::Table.new(table_name_and_alias, reflection.klass.arel_engine)
  19. end
  20. end
  21.  
  22. diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
  23. index 07c5545..17b2b9b 100755
  24. --- a/activerecord/lib/active_record/base.rb
  25. +++ b/activerecord/lib/active_record/base.rb
  26. @@ -1506,7 +1506,15 @@ module ActiveRecord #:nodoc:
  27. end
  28.  
  29. def arel_table(table = nil)
  30. - Relation.new(self, Arel::Table.new(table || table_name))
  31. + table = Arel::Table.new(table || table_name, arel_engine)
  32. +
  33. + relation = Relation.new(self, table)
  34. + relation.table = table
  35. + relation
  36. + end
  37. +
  38. + def arel_engine
  39. + @arel_engine ||= Arel::Sql::Engine.new(self)
  40. end
  41.  
  42. private
  43. diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
  44. index ae03e1d..65462e8 100644
  45. --- a/activerecord/lib/active_record/relation.rb
  46. +++ b/activerecord/lib/active_record/relation.rb
  47. @@ -6,7 +6,7 @@ module ActiveRecord
  48. delegate :length, :collect, :map, :each, :all?, :to => :to_a
  49.  
  50. attr_reader :relation, :klass, :preload_associations, :eager_load_associations
  51. - attr_writer :readonly, :preload_associations, :eager_load_associations
  52. + attr_writer :readonly, :preload_associations, :eager_load_associations, :table
  53.  
  54. def initialize(klass, relation)
  55. @klass, @relation = klass, relation
  56. @@ -57,7 +57,8 @@ module ActiveRecord
  57. :conditions => where_clause,
  58. :limit => @relation.taken,
  59. :offset => @relation.skipped,
  60. - :from => (@relation.send(:from_clauses) if @relation.send(:sources).present?)
  61. + :from => (@relation.send(:from_clauses) if @relation.send(:sources).present?),
  62. + :relation => table
  63. },
  64. ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, @eager_load_associations, nil))
  65. rescue ThrowResult
  66. @@ -129,9 +130,14 @@ module ActiveRecord
  67. relation.readonly = @readonly
  68. relation.preload_associations = @preload_associations
  69. relation.eager_load_associations = @eager_load_associations
  70. + relation.table = table
  71. relation
  72. end
  73.  
  74. + def table
  75. + @table ||= Arel::Table.new(@klass.table_name, @klass.arel_engine)
  76. + end
  77. +
  78. protected
  79.  
  80. def method_missing(method, *args, &block)
  81. @@ -157,5 +163,9 @@ module ActiveRecord
  82. @relation.send(:where_clauses).join(join_string)
  83. end
  84.  
  85. + def primary_key
  86. + @klass.primary_key
  87. + end
  88. +
  89. end
  90. end
  91. diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
  92. index 7a1d6fc..56a51e3 100644
  93. --- a/activerecord/lib/active_record/relation/finder_methods.rb
  94. +++ b/activerecord/lib/active_record/relation/finder_methods.rb
  95. @@ -78,7 +78,7 @@ module ActiveRecord
  96. end
  97.  
  98. def find_one(id)
  99. - record = where(@klass.primary_key => id).first
  100. + record = where(table[primary_key].eq(id)).first
  101.  
  102. unless record
  103. conditions = where_clause(', ')
  104. @@ -90,7 +90,8 @@ module ActiveRecord
  105. end
  106.  
  107. def find_some(ids)
  108. - result = where(@klass.primary_key => ids).all
  109. + predicate = Arel::Predicates::In.new(table[primary_key], ids)
  110. + result = where(predicate).all
  111.  
  112. expected_size =
  113. if @relation.taken && ids.size > @relation.taken
Add Comment
Please, Sign In to add comment