aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-07-11 06:09:08 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-07-11 06:09:08 +0000
commit5b9b904f149f8ae41255096ba7b210c912329103 (patch)
treeba7c8c0e9c97024503b962158499d78bcd688096 /activerecord/lib/active_record
parent16669774903dd7bfaaf1bf8f0c72e49ebaeffeeb (diff)
downloadrails-5b9b904f149f8ae41255096ba7b210c912329103.tar.gz
rails-5b9b904f149f8ae41255096ba7b210c912329103.tar.bz2
rails-5b9b904f149f8ae41255096ba7b210c912329103.zip
Added support for limit and offset with eager loading of has_one and belongs_to associations. Using the options with has_many and has_and_belongs_to_many associations will now raise an ActiveRecord::ConfigurationError #1692 [Rick Olsen]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1811 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb36
-rwxr-xr-xactiverecord/lib/active_record/base.rb4
2 files changed, 31 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 6f49800df6..ffb0d5679b 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -168,7 +168,8 @@ module ActiveRecord
# catch-all for performance problems, but its a great way to cut down on the number of queries in a situation as the one described above.
#
# Please note that because eager loading is fetching both models and associations in the same grab, it doesn't make sense to use the
- # :limit property and it will be ignored if attempted.
+ # :limit and :offset options on has_many and has_and_belongs_to_many associations and an ConfigurationError exception will be raised
+ # if attempted. It does, however, work just fine with has_one and belongs_to associations.
#
# Also have in mind that since the eager loading is pulling from multiple tables, you'll have to disambiguate any column references
# in both conditions and orders. So :order => "posts.id DESC" will work while :order => "id DESC" will not. This may require that
@@ -740,12 +741,9 @@ module ActiveRecord
def find_with_associations(options = {})
reflections = reflect_on_included_associations(options[:include])
- reflections.each do |r|
- raise(
- NoMethodError,
- "Association was not found; perhaps you misspelled it? You specified :include=>:#{options[:include].join(', :')}"
- ) if r.nil?
- end
+
+ guard_against_missing_reflections(reflections, options)
+ guard_against_unlimitable_reflections(reflections, options)
schema_abbreviations = generate_schema_abbreviations(reflections)
primary_key_table = generate_primary_key_table(reflections, schema_abbreviations)
@@ -788,6 +786,24 @@ module ActiveRecord
[ associations ].flatten.collect { |association| reflect_on_association(association) }
end
+ def guard_against_missing_reflections(reflections, options)
+ reflections.each do |r|
+ raise(
+ ConfigurationError,
+ "Association was not found; perhaps you misspelled it? You specified :include => :#{options[:include].join(', :')}"
+ ) if r.nil?
+ end
+ end
+
+ def guard_against_unlimitable_reflections(reflections, options)
+ if (options[:offset] || options[:limit]) && !using_limitable_reflections?(reflections)
+ raise(
+ ConfigurationError,
+ "You can not use offset and limit together with has_many or has_and_belongs_to_many associations"
+ )
+ end
+ end
+
def generate_schema_abbreviations(reflections)
schema = [ [ table_name, column_names ] ]
schema += reflections.collect { |r| [ r.table_name, r.klass.column_names ] }
@@ -830,10 +846,14 @@ module ActiveRecord
add_conditions!(sql, options[:conditions])
add_sti_conditions!(sql, reflections)
sql << "ORDER BY #{options[:order]} " if options[:order]
-
+ add_limit!(sql, options) if using_limitable_reflections?(reflections)
return sanitize_sql(sql)
end
+ def using_limitable_reflections?(reflections)
+ reflections.reject { |r| [ :belongs_to, :has_one ].include?(r.macro) }.length.zero?
+ end
+
def add_sti_conditions!(sql, reflections)
sti_sql = ""
reflections.each do |reflection|
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index b32843d069..be64c1bbd3 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -26,6 +26,8 @@ module ActiveRecord #:nodoc:
end
class StaleObjectError < ActiveRecordError #:nodoc:
end
+ class ConfigurationError < StandardError #:nodoc:
+ end
class AttributeAssignmentError < ActiveRecordError #:nodoc:
attr_reader :exception, :attribute
@@ -336,7 +338,7 @@ module ActiveRecord #:nodoc:
case args.first
when :first
- find(:all, options.merge({ :limit => 1 })).first
+ find(:all, options.merge(options[:include] ? { } : { :limit => 1 })).first
when :all
options[:include] ? find_with_associations(options) : find_by_sql(construct_finder_sql(options))
else