Skip to content

Commit

Permalink
Make Model.find(:first, ..) use relations
Browse files Browse the repository at this point in the history
  • Loading branch information
lifo committed Dec 27, 2009
1 parent 59cf5e7 commit d6d0fe8
Showing 1 changed file with 2 additions and 24 deletions.
26 changes: 2 additions & 24 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -645,7 +645,7 @@ def find(*args)


case args.first case args.first
when :first when :first
find_initial(options) construct_finder_arel_with_includes(options).first
when :last when :last
find_last(options) find_last(options)
when :all when :all
Expand Down Expand Up @@ -1519,11 +1519,6 @@ def arel_table(table = nil)
end end


private private
def find_initial(options)
options.update(:limit => 1)
find_every(options).first
end

def find_last(options) def find_last(options)
order = options[:order] order = options[:order]


Expand All @@ -1540,7 +1535,7 @@ def find_last(options)
end end


begin begin
find_initial(options.merge({ :order => order })) construct_finder_arel_with_includes(options).order(order).first
ensure ensure
scope[:order] = original_scoped_order if original_scoped_order scope[:order] = original_scoped_order if original_scoped_order
end end
Expand All @@ -1558,23 +1553,6 @@ def reverse_sql_order(order_query)
}.join(',') }.join(',')
end end


def find_every(options)
include_associations = merge_includes(scope(:find, :include), options[:include])

if include_associations.any? && references_eager_loaded_tables?(options)
records = find_with_associations(options)
else
records = find_by_sql(construct_finder_sql(options))
if include_associations.any?
preload_associations(records, include_associations)
end
end

records.each { |record| record.readonly! } if options[:readonly]

records
end

# Finder methods must instantiate through this method to work with the # Finder methods must instantiate through this method to work with the
# single-table inheritance model that makes it possible to create # single-table inheritance model that makes it possible to create
# objects of different types from the same table. # objects of different types from the same table.
Expand Down

0 comments on commit d6d0fe8

Please sign in to comment.