How to make 'will_paginate' lazyload in rails 3?
In rails3, almost all query interface change to be lazylo开发者_StackOverflowading now, but 'will_paginate' will hit the database imediately after you use the 'paginate' method. How can I make it lazyload records?
I have make it by rewriting many code in will_paginate, It's really hard for will_paginate to lazy load, because will_paginate has a core class 'WillPaginate::Collection' which is a subclass of 'Array'.(I don't like this class )
All results generated by 'paginate' method will return a Collection instance, so it will force the 'ActiveRecord::Relation' object into an 'Array' which will force the Relation hit the database imidiately.
I've created a very lightweight plugin that uses limit(per_page).offset(page*per_page) to implement pagination under Rails 3. Therefore, it provides lazy loading, which is great for fragment caching. Interface is just the same as will_paginate (@posts.recent.paginate :page=>params[:page]). There are not too many view helpers yet, but I'm sure you will find it useful: http://github.com/josei/simple_paginate
精彩评论