#93 ✓resolved
chris

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 => 

[&quot;(employee_id = ? AND state = 'assigned') OR
  (employee_id IS NULL AND state = 'open')&quot;, employee.id]} } </code>



Comments and changes to this ticket

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.

New-ticket Create new ticket

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.

People watching this ticket

Attachments

Referenced by

Pages