Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

default_scope is having a side affecct on create

#3218

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

Reported by johnf · September 16th, 2009 @ 03:59 PM

State: committed
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: none

Activity

  1. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from activerecord, default_scope to activerecord, bugmash, default_scope

    September 25th, 2009 @ 12:05 PM

  2. hsume2 (Henry)
  3. Elad Meidar
    Elad Meidar

    +1 verified on 2-3-stable, i guess i am lucky for not running into it up until now.

    Attached patches for 2-3-stable and master

    I generally allowed the scoped attributes to override existing attributes only:

    if the attribute value is #blank?

    if the attribute is not blank, but the value was initiated via a column default definition.

    otherwise the value is not overridden.

    September 25th, 2009 @ 10:56 PM

  4. Elad Meidar
    Elad Meidar

    heh, silly Lighthouse textile :)

    September 25th, 2009 @ 10:56 PM

  5. hsume2 (Henry)
    hsume2 (Henry)

    I think I get what you've done. However, I think your implementation conficts with #test_default_values_on_empty_strings in base_test.rb. I've attached a patch (with an additional fail test) that fixes it. (Should apply cleanly to master and 2-3-stable)

    Also, note: expecting 50000, as opposed to the default value of 70000 defined in the schema.

    September 26th, 2009 @ 01:47 AM

  6. hsume2 (Henry)
    hsume2 (Henry)

    Sorry, I don't mean conflict.. exactly. More like, the behavior is different from that in #test_default_values_on_empty_strings.

    September 26th, 2009 @ 01:48 AM

  7. Elad Meidar
    Elad Meidar
    • Tag changed from activerecord, bugmash, default_scope to activerecord, bugmash, bugmash-review, default_scope

    September 26th, 2009 @ 01:53 AM

  8. Elad Meidar
    Elad Meidar

    Mmmm,
    by doing

    -        self.attributes = attributes unless attributes.nil?

         self.class.send(:scope, :create).each { |att,value| self.send(&quot;#{att}=&quot;, value) } if self.class.send(:scoped?, :create)
    
    
    
    
    •  self.attributes = attributes unless attributes.nil?</code>
      
    
    
    
    
    you are overriding the attributes from the scope.... the order of steps i did was to ensure that the last value that the user specified, takes affect.
    1. default db column value
    2. scoped :conditions
    3. initialize/find parameters
    
    
    

    September 26th, 2009 @ 02:02 AM

  9. Elad Meidar
    Elad Meidar

    Mmmm,
    by doing

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

    you are overriding the attributes from the scope.... the order of steps i did was to ensure that the last value that the user specified, takes affect.

    1. default db column value
    2. scoped :conditions
    3. initialize/find parameters

    September 26th, 2009 @ 02:03 AM

  10. Elad Meidar
    Elad Meidar

    +1 on @hsume2' patch, solves it in a much cleaner way than my patches

    September 26th, 2009 @ 03:05 AM

  11. David Trasbo
    David Trasbo

    +1 though the patch no longer applies to 2-3-stable.

    September 26th, 2009 @ 05:20 PM

  12. Elad Meidar
    Elad Meidar

    @david, which one did you try?

    September 26th, 2009 @ 05:59 PM

  13. Josh Sharpe
    Josh Sharpe

    +1 to hsume2's master patch

    Here's the patch for 2-3 which is basically identical to the master patch.

    September 26th, 2009 @ 06:08 PM

  14. sr.iniv.t
    sr.iniv.t

    +1 verified.

    Both patches (hsume2's on master and Josh's on 2-3) apply cleanly and all tests pass.

    September 27th, 2009 @ 04:55 AM

  15. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from activerecord, bugmash, bugmash-review, default_scope to activerecord, bugmash-review, default_scope

    September 27th, 2009 @ 12:06 PM

  16. José Valim
    José Valim
    • Assigned user set to José Valim
    • Milestone cleared.

    February 21st, 2010 @ 11:52 AM

  17. José Valim
    José Valim
    • State changed from new to committed

    Applied on master. Can someone please rebase the patch for 2-3-stable?

    February 26th, 2010 @ 10:10 AM

  18. Repository
  19. Rizwan Reza
    Rizwan Reza
    • Tag changed from activerecord, bugmash-review, default_scope to activerecord, default_scope

    May 15th, 2010 @ 06:40 PM

  20. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to

    October 15th, 2010 @ 11:01 PM