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.

using with scope_out

#70

I've been using scope_out to define finders on my models

class Task < ActiveRecord::Base

	scope_out       :recent_incomplete,  	:conditions => {:completed => nil}, 
	                                  		:order => 'tasks.created_at DESC',
	                                  		:include => :user
end

and I also have a User model which has many tasks

class User < ActiveRecord::Base

	has_many :tasks
	
end

The database table has a range of tasks for different users, some complete, some not.

and I was wanting to use these with will_paginate so I could do the following in my controller:

  @all_incomplete_tasks = Task.paginate_recent_incomplete(:all, :page => params[:page])

  @user_incomplete_tasks = current_user.tasks.paginate_recent_incomplete(:all, :page => params[:page])

However, the problem I found was that although the correct records were returned by the call to paginate_recent_incomplete, the total number of records (and hence the number of pages) was incorrect as it ignores the scoping implied by scope_out (wp_count! doesn't take this into account).

I've updated the code for wp_count! so it handles the above syntax when used with scope_out and doesn't break the existing tests. I've, however, not added any tests to validate the code I've added. I'm only just starting to learn about testing so that's a bit beyond me for the moment.

Not sure if this is a sensible thing to do or not - patch attached for your perusal though

Cheers

Rupert

Reported by Deleted User · August 9th, 2007 @ 12:43 AM

State: resolved
Milestone: none
Assigned to: Mislav Mislav

Activity

  1. Chris Wanstrath
    Chris Wanstrath
    • State changed from new to open
    • Assigned user changed from Chris Wanstrath to Mislav

    August 9th, 2007 @ 12:43 AM

  2. Deleted User
    Deleted User

    I've just updated the patch....

    with_scope adds a method which can be used like with_scope to use the defined scope.

    So in the example above a method Task.with_recent_incomplete is added. In the new version of the patch, if this method exists it evaluates the count within it.

    Still not sure if this is a good idea or not ...dependencies between plugins, but it's proved useful for me!

    Cheers

    Rupert

    August 9th, 2007 @ 12:43 AM

  3. Jack Danger
    Jack Danger

    +1 for this patch.

    Scope out is killer useful. So's will paginate. Any chance we can get them to play nice?

    August 9th, 2007 @ 12:43 AM

  4. Chris Wanstrath
    Chris Wanstrath
    • State changed from open to resolved

    (from [326]) will_paginate: add in support for the scope_out plugin [rupertv, Chris Wanstrath] [#70 state:resolved]

    http://plugins.require.errtheblo...

    August 18th, 2007 @ 05:04 AM

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.