diff options
author | Neeraj Singh <neerajdotname@gmail.com> | 2010-08-12 21:37:48 -0400 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-08-19 14:52:15 -0300 |
commit | 2e455429427a4078d2888cc39305f951bdf1e643 (patch) | |
tree | b67aa66e66704aca116fd90f72144528aab563b4 /activerecord/lib | |
parent | 43f44c1a034497fef0a9ed64e0da6f090b5c3b7e (diff) | |
download | rails-2e455429427a4078d2888cc39305f951bdf1e643.tar.gz rails-2e455429427a4078d2888cc39305f951bdf1e643.tar.bz2 rails-2e455429427a4078d2888cc39305f951bdf1e643.zip |
While creating a new record using has_many create method default scope of child should be respected.
author.posts.create should take into account default_scope
defined on post.
[#3939: state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 7 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 11 |
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 |