
Order of named_scope
Reported by chris | June 21st, 2009 @ 11:11 AM
It seems to me that the order in which the named scopes are applied does matter:
>> s = Task.open_or_assigned_to(employee).search
=> #<Searchlogic::Search:0x215f598 @current_scope={:conditions=>["(employee_id = ? AND state = 'assigned') OR \n (employee_id IS NULL AND state = 'open')", 2]}, @klass=Task(id: integer, name: string, due_date: date, creator_id: integer, project_id: integer, employee_id: integer, state: string, created_at: datetime, updated_at: datetime)>
>> s.completed
=> nil
>> s = Task.completed.search
=> #<Searchlogic::Search:0x215afd4 @current_scope={:conditions=>{:state=>"completed"}}, @klass=Task(id: integer, name: string, due_date: date, creator_id: integer, project_id: integer, employee_id: integer, state: string, created_at: datetime, updated_at: datetime)>
>> s.open_or_assigned_to(employee)
=> #<Searchlogic::Search:0x215afd4 @conditions={:open_or_assigned_to=>#<Employee id: 2, first_name: "Christoph", last_name: "Lipautz", active: true, user_id: 2, created_at: "2009-06-20 20:15:25", updated_at: "2009-06-20 20:15:25">}, @current_scope={:conditions=>{:state=>"completed"}}, @klass=Task(id: integer, name: string, due_date: date, creator_id: integer, project_id: integer, employee_id: integer, state: string, created_at: datetime, updated_at: datetime)>
However,
Task.open_or_assigned_to(employee).completed.search and
Task.completed.open_or_assigned_to(employee).search both work
fine.
The named scope open_or_assigned_to looks like this:
named_scope :open_or_assigned_to, lambda { |employee| { :conditions =>
["(employee_id = ? AND state = 'assigned') OR
(employee_id IS NULL AND state = 'open')", employee.id]} }
Comments and changes to this ticket
-
Ben Johnson June 24th, 2009 @ 02:12 AM
- State changed from new to open
I'm confused as to what the problem is. The @current_scope is passed in right from active record, then I just apply that first, then add in the rest of your conditions. So in your examples the current scope should be applied first. Also, I see what you are saying regarding the order on the other conditions. I think the best way to do that would be to use an ordered hash internally for the conditions.
-
Ben Johnson July 30th, 2009 @ 02:31 PM
- State changed from open to resolved
Yeah, I ran you tests. Doing this doesnt do anything:
search.five_year_old_leo
That's just returning the value of five_year_old_leo, which is nil. You need to do:
search.five_year_old_leo = true
or:
search.five_year_old_leo(true)
Hope that helps.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
Provides common named scopes and object based searching.