This project is archived and is in readonly mode.

#3218 ✓committed
johnf

default_scope is having a side affecct on create

Reported by johnf | September 16th, 2009 @ 03:59 PM | in 3.0.2

If I have the following

class Foo
  default_scope :conditions => {:bar => 1}
end

If I try to create a new object

>> Foo.new(:bar => 5)
=> #<Foo id: nil, name: nil, bar: 1, created_at: nil, updated_at: nil>

The object now has bar set to one when I've explicitly asked for it to be set to 5.

It makes sense to set the default if I don't pass it in but not otherwise

At a rough guess in ActiveRecord::Base initialize

2438         self.attributes = attributes unless attributes.nil?
2439         self.class.send(:scope, :create).each { |att,value| self.send("#{att}=", value) } if self.class.send(:scoped?, :create)

should be swapped around

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages