aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb7
-rw-r--r--activerecord/lib/active_record/relation.rb11
2 files changed, 13 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index b5159eead3..132e9cf882 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -492,7 +492,12 @@ module ActiveRecord
def create_record(attrs)
attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash)
ensure_owner_is_not_new
- record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) do
+
+ _scope = self.construct_scope[:create]
+ csm = @reflection.klass.send(:current_scoped_methods)
+ options = (csm.blank? || !_scope.is_a?(Hash)) ? _scope : _scope.merge(csm.where_values_hash)
+
+ record = @reflection.klass.send(:with_scope, :create => options) do
@reflection.build_association(attrs)
end
if block_given?
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index cfe4d23965..03b06205d4 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -316,16 +316,19 @@ module ActiveRecord
@to_sql ||= arel.to_sql
end
- def scope_for_create
- @scope_for_create ||= begin
- @create_with_value || Hash[
- @where_values.find_all { |w|
+ def where_values_hash
+ Hash[@where_values.find_all { |w|
w.respond_to?(:operator) && w.operator == :==
}.map { |where|
[where.operand1.name,
where.operand2.respond_to?(:value) ?
where.operand2.value : where.operand2]
}]
+ end
+
+ def scope_for_create
+ @scope_for_create ||= begin
+ @create_with_value || where_values_hash
end
end