aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations.rb')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb42
1 files changed, 10 insertions, 32 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index b52b298027..ebf1a41e85 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -774,13 +774,12 @@ module ActiveRecord
# [collection.build(attributes = {}, ...)]
# Returns one or more new objects of the collection type that have been instantiated
# with +attributes+ and linked to this object through a foreign key, but have not yet
- # been saved. <b>Note:</b> This only works if an associated object already exists, not if
- # it's +nil+!
+ # been saved.
# [collection.create(attributes = {})]
# Returns a new object of the collection type that has been instantiated
# with +attributes+, linked to this object through a foreign key, and that has already
- # been saved (if it passed the validation). <b>Note:</b> This only works if an associated
- # object already exists, not if it's +nil+!
+ # been saved (if it passed the validation). *Note*: This only works if the base model
+ # already exists in the DB, not if it is a new (unsaved) record!
#
# (*Note*: +collection+ is replaced with the symbol passed as the first argument, so
# <tt>has_many :clients</tt> would add among others <tt>clients.empty?</tt>.)
@@ -1040,7 +1039,6 @@ module ActiveRecord
# A Post class declares <tt>belongs_to :author</tt>, which will add:
# * <tt>Post#author</tt> (similar to <tt>Author.find(author_id)</tt>)
# * <tt>Post#author=(author)</tt> (similar to <tt>post.author_id = author.id</tt>)
- # * <tt>Post#author?</tt> (similar to <tt>post.author == some_author</tt>)
# * <tt>Post#build_author</tt> (similar to <tt>post.author = Author.new</tt>)
# * <tt>Post#create_author</tt> (similar to <tt>post.author = Author.new; post.author.save; post.author</tt>)
# The declaration can also include an options hash to specialize the behavior of the association.
@@ -1703,30 +1701,19 @@ module ActiveRecord
end
def construct_finder_arel_with_included_associations(options, join_dependency)
- relation = active_relation
+ relation = scoped
for association in join_dependency.join_associations
relation = association.join_relation(relation)
end
- relation = relation.joins(options[:joins]).
- select(column_aliases(join_dependency)).
- group(options[:group]).
- having(options[:having]).
- order(options[:order]).
- where(options[:conditions]).
- from(options[:from])
+ relation = relation.apply_finder_options(options).select(column_aliases(join_dependency))
- scoped_relation = current_scoped_methods
- scoped_relation_limit = scoped_relation.taken if scoped_relation
-
- relation = current_scoped_methods.except(:limit).merge(relation) if current_scoped_methods
-
- if !using_limitable_reflections?(join_dependency.reflections) && ((scoped_relation && scoped_relation.taken) || options[:limit])
+ if !using_limitable_reflections?(join_dependency.reflections) && relation.limit_value
relation = relation.where(construct_arel_limited_ids_condition(options, join_dependency))
end
- relation = relation.limit(options[:limit] || scoped_relation_limit) if using_limitable_reflections?(join_dependency.reflections)
+ relation = relation.except(:limit, :offset) unless using_limitable_reflections?(join_dependency.reflections)
relation
end
@@ -1754,23 +1741,14 @@ module ActiveRecord
end
def construct_finder_sql_for_association_limiting(options, join_dependency)
- relation = active_relation
+ relation = scoped
for association in join_dependency.join_associations
relation = association.join_relation(relation)
end
- relation = relation.joins(options[:joins]).
- where(options[:conditions]).
- group(options[:group]).
- having(options[:having]).
- order(options[:order]).
- limit(options[:limit]).
- offset(options[:offset]).
- from(options[:from])
-
- relation = current_scoped_methods.except(:select, :includes, :eager_load).merge(relation) if current_scoped_methods
- relation = relation.select(connection.distinct("#{connection.quote_table_name table_name}.#{primary_key}", options[:order]))
+ relation = relation.apply_finder_options(options).except(:select)
+ relation = relation.select(connection.distinct("#{connection.quote_table_name table_name}.#{primary_key}", relation.order_values.join(", ")))
relation.to_sql
end