1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add relation#merge to merge two relations

This commit is contained in:
Pratik Naik 2009-12-28 01:08:34 +05:30
parent 08312e9958
commit a8b10a2a8d
2 changed files with 25 additions and 5 deletions

View file

@ -12,6 +12,18 @@ module ActiveRecord
@loaded = false
end
def merge(r)
joins(r.relation.joins(r.relation)).
group(r.send(:group_clauses).join(', ')).
order(r.send(:order_clauses).join(', ')).
where(r.send(:where_clause)).
limit(r.taken).
offset(r.skipped).
select(r.send(:select_clauses).join(', '))
end
alias :& :merge
def preload(*associations)
create_new_relation(@relation, @readonly, @associations_to_preload + Array.wrap(associations))
end
@ -25,7 +37,7 @@ module ActiveRecord
end
def select(selects)
create_new_relation(@relation.project(selects))
selects.present? ? create_new_relation(@relation.project(selects)) : create_new_relation
end
# TODO : This is temporary. We need .from in Arel.
@ -37,11 +49,11 @@ module ActiveRecord
end
def group(groups)
create_new_relation(@relation.group(groups))
groups.present? ? create_new_relation(@relation.group(groups)) : create_new_relation
end
def order(orders)
create_new_relation(@relation.order(orders))
orders.present? ? create_new_relation(@relation.order(orders)) : create_new_relation
end
def reverse_order
@ -57,11 +69,11 @@ module ActiveRecord
end
def limit(limits)
create_new_relation(@relation.take(limits))
limits.present? ? create_new_relation(@relation.take(limits)) : create_new_relation
end
def offset(offsets)
create_new_relation(@relation.skip(offsets))
offsets.present? ? create_new_relation(@relation.skip(offsets)) : create_new_relation
end
def on(join)

View file

@ -328,4 +328,12 @@ class RelationTest < ActiveRecord::TestCase
assert davids.loaded?
end
def test_relation_merging
devs = Developer.where("salary >= 80000") & Developer.limit(2) & Developer.order('id ASC').where("id < 3")
assert_equal [developers(:david), developers(:jamis)], devs.to_a
dev_with_count = Developer.limit(1) & Developer.order('id DESC') & Developer.select('developers.*, count(id) id_count').group('id')
assert_equal [developers(:poor_jamis)], dev_with_count.to_a
assert_equal 1, dev_with_count.first.id_count.to_i
end
end