Guest User

Untitled

a guest
Apr 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. module ActiveRecord
  2. module Associations
  3. class HasManyThroughAssociation < AssociationProxy
  4.  
  5. def construct_owner_attributes_with_bugfix(reflection)
  6. if reflection.macro == :belongs_to
  7. { reflection.klass.primary_key => \
  8. @owner[reflection.primary_key_name] }
  9. else
  10. construct_quoted_owner_attributes_without_bugfix(reflection)
  11. end
  12. end
  13. alias_method_chain :construct_owner_attributes, :bugfix
  14.  
  15. def construct_quoted_owner_attributes_with_bugfix(reflection)
  16. if reflection.macro == :belongs_to
  17. { reflection.klass.primary_key => \
  18. @owner.send(:attributes_with_quotes)[reflection.primary_key_name] }
  19. else
  20. construct_quoted_owner_attributes_without_bugfix(reflection)
  21. end
  22. end
  23. alias_method_chain :construct_quoted_owner_attributes, :bugfix
  24.  
  25. end
  26.  
  27. module ClassMethods
  28. class JoinDependency
  29. class JoinAssociation < JoinBase
  30.  
  31. def association_join_with_bugfix
  32. res = association_join_without_bugfix
  33.  
  34. return res unless reflection.macro == :has_many and \
  35. reflection.options[:through] and \
  36. reflection.through_reflection.macro == :belongs_to
  37.  
  38. connection = reflection.active_record.connection
  39.  
  40. if through_reflection.options[:as]
  41. jt_foreign_key = through_reflection.options[:as].to_s + '_id'
  42. else
  43. jt_foreign_key = through_reflection.primary_key_name
  44. end
  45.  
  46. erroneous_string = " ON (%s.%s = %s.%s" % [
  47. connection.quote_table_name(parent.aliased_table_name),
  48. connection.quote_column_name(parent.primary_key),
  49. connection.quote_table_name(aliased_join_table_name),
  50. connection.quote_column_name(jt_foreign_key)
  51. ]
  52.  
  53. if (index = res.index(erroneous_string))
  54. correct_string = " ON (%s.%s = %s.%s" % [
  55. connection.quote_table_name(parent.aliased_table_name),
  56. connection.quote_column_name(jt_foreign_key),
  57. connection.quote_table_name(aliased_join_table_name),
  58. connection.quote_column_name(
  59. through_reflection.klass.primary_key
  60. )
  61. ]
  62. res[index, erroneous_string.length] = correct_string
  63. end
  64.  
  65. res
  66. end
  67. alias_method_chain :association_join, :bugfix
  68.  
  69. end
  70. end
  71. end
  72.  
  73. end
  74. end
Add Comment
Please, Sign In to add comment