Commit 691deee5 by Ivan Lan

Add distinct to association

parent da399723
...@@ -9,7 +9,7 @@ module ActsAsPasting ...@@ -9,7 +9,7 @@ module ActsAsPasting
# ary => [[klass, id], [klass, id]] # ary => [[klass, id], [klass, id]]
# ary => [obj, obj] # ary => [obj, obj]
scope :pasted_with_any, ->(ary, prefix: '') { scope :pastable_with_any, ->(ary, prefix: '') {
ary = parsed_condition_ary(ary) ary = parsed_condition_ary(ary)
arel = Arel::Table.new(:acts_as_pasting_pastings, as: "acts_as_pasting_pastings_#{SecureRandom.hex(10)}") arel = Arel::Table.new(:acts_as_pasting_pastings, as: "acts_as_pasting_pastings_#{SecureRandom.hex(10)}")
source_arel = arel_table source_arel = arel_table
...@@ -23,10 +23,10 @@ module ActsAsPasting ...@@ -23,10 +23,10 @@ module ActsAsPasting
joins(arel.join(arel).on(join_condition).join_sources).distinct joins(arel.join(arel).on(join_condition).join_sources).distinct
} }
scope :pasted_with_all, ->(ary, prefix: '') { scope :pastable_with_all, ->(ary, prefix: '') {
ary = parsed_condition_ary(ary) ary = parsed_condition_ary(ary)
ary.reduce(where(nil)) do |association, value| ary.reduce(where(nil)) do |association, value|
association.pasted_with_any([value], prefix: prefix) association.pastable_with_any([value], prefix: prefix)
end end
} }
end end
...@@ -35,7 +35,7 @@ module ActsAsPasting ...@@ -35,7 +35,7 @@ module ActsAsPasting
def acts_as_pastable associations, prefix: '', **options def acts_as_pastable associations, prefix: '', **options
pasting_name = prefix.blank? ? "#{associations}_pastings".to_sym : [associations, prefix, 'pastings'].join('_').to_sym pasting_name = prefix.blank? ? "#{associations}_pastings".to_sym : [associations, prefix, 'pastings'].join('_').to_sym
has_many(pasting_name, -> { where(type: prefix) }, as: :pasteable, class_name: 'ActsAsPasting::Pasting') has_many(pasting_name, -> { where(type: prefix) }, as: :pasteable, class_name: 'ActsAsPasting::Pasting')
has_many(associations, through: pasting_name, source: :pasted, **options) has_many(associations, -> { distinct }, through: pasting_name, source: :pasted, **options)
end end
end end
end end
......
...@@ -204,7 +204,7 @@ module ActsAsPasting ...@@ -204,7 +204,7 @@ module ActsAsPasting
def acts_as_pasted associations, prefix: '', **options def acts_as_pasted associations, prefix: '', **options
pasting_name = prefix.blank? ? "#{associations}_pastings".to_sym : [associations, prefix, 'pastings'].join('_').to_sym pasting_name = prefix.blank? ? "#{associations}_pastings".to_sym : [associations, prefix, 'pastings'].join('_').to_sym
has_many(pasting_name, -> { where(type: prefix) }, class_name: 'ActsAsPasting::Pasting', as: :pasted) has_many(pasting_name, -> { where(type: prefix) }, class_name: 'ActsAsPasting::Pasting', as: :pasted)
has_many(associations, through: pasting_name, source: :pasteable, **options) has_many(associations, -> { distinct }, through: pasting_name, source: :pasteable, **options)
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment