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 should ignore string conditions on new/create

#2654

Using a condition string like {:conditions => "state != 'active'"} in a default_scope is not possible because it expects a hash to assign the values into the model attributes when initializing.

The following error is raised when it tries to call the string conditions as an attribute assignment:

1)
undefined method `state != 'active'=' for #<Model:0x33bac74>

This patch checks the default_scope options and only assigns the values to the model if the conditions option is a Hash. If it's a string, it's ignored.

This means that if you use a string for default_scope conditions and you expect default values, you should set these in an initialize method or else before_create.

(This patch is against 2.3-stable)

Reported by Andrew Vit · May 15th, 2009 @ 08:48 PM

State: duplicate
Milestone: 2.x
Assigned to: Manfred Stienstra Manfred Stienstra
Importance: none

Activity

  1. Andrew Vit
    Andrew Vit

    Don't know what happened with my patch file there... This is the correct one.

    May 15th, 2009 @ 09:17 PM

  2. Andrew Vit
  3. Andrew Vit
  4. Andrew Vit
    Andrew Vit
    • Assigned user set to Manfred Stienstra

    Also #2371 Default scope with :conditions => "a string".

    Manfred, I hear you're the one working on a scopes rewrite: so I'm assigning this to you.

    Any chance of a quick-fix for this in 2.3 before the rewrite?

    May 18th, 2009 @ 12:44 AM

  5. Andrew Vit
    Andrew Vit

    Looks like this is now fixed by #2181 Default Scope with conditions on create
    97b75c9f1668992bd246fe46235c5764a4fa6e58

    Please close.

    May 19th, 2009 @ 06:39 PM

  6. Pratik
    Pratik
    • Title changed from [Patch] default_scope should ignore string conditions on new/create to default_scope should ignore string conditions on new/create
    • State changed from new to duplicate

    May 19th, 2009 @ 07:04 PM