Comments on: Rails Pagination by Letters (not Numbers like will_paginate) http://www.glennfu.com/2008/06/24/rails-pagination-by-letters-not-numbers-like-will_paginate/ code like water, compile like bamboo! Tue, 25 Jan 2011 15:52:41 +0000 hourly 1 http://wordpress.org/?v=3.2.1 By: Gleb Pereyaslavsky http://www.glennfu.com/2008/06/24/rails-pagination-by-letters-not-numbers-like-will_paginate/comment-page-1/#comment-79 Gleb Pereyaslavsky Mon, 14 Sep 2009 22:10:16 +0000 http://www.glennfu.com/?p=36#comment-79 Model.approved is my another named_scope, so just ignore it. Model.approved is my another named_scope, so just ignore it.

]]>
By: Gleb Pereyaslavsky http://www.glennfu.com/2008/06/24/rails-pagination-by-letters-not-numbers-like-will_paginate/comment-page-1/#comment-78 Gleb Pereyaslavsky Mon, 14 Sep 2009 22:07:00 +0000 http://www.glennfu.com/?p=36#comment-78 Thank you for the code, it appeared to be very useful. I could suggest a slight refactoring, though. The controller's code, Model.find( ... ) to be changed into model's named scopes named_scope :non_alphabetical, {:conditions => ["name REGEXP ?", "^[^a-z]"], :order => 'name'} named_scope :starting_with, lambda{|letter|{:conditions => ["name LIKE ?", "#{letter}%"], :order => 'name'}} and use this way: if params[:letter] == '#' @data = Model.approved.non_alphabetical else @data = Model.approved.starting_with params[:letter] end This way it become more readable. One more thing regarding performance, it would be useful to create an indexed column containing the first letter of the model name, and to make all selects using this column. Also the block with the list of letters could be moved into partial to be used multiple times (on top and bottom for example). Thank you for the code, it appeared to be very useful. I could suggest a slight refactoring, though. The controller’s code,

Model.find( … )

to be changed into model’s named scopes

named_scope :non_alphabetical, {:conditions => ["name REGEXP ?", "^[^a-z]“], :o rder => ‘name’}
named_scope :starting_with, lambda{|letter|{:conditions => ["name LIKE ?", "#{letter}%"], :o rder => ‘name’}}

and use this way:

if params[:letter] == ‘#’
@data = Model.approved.non_alphabetical
else
@data = Model.approved.starting_with params[:letter]
end

This way it become more readable.

One more thing regarding performance, it would be useful to create an indexed column containing the first letter of the model name, and to make all selects using this column.

Also the block with the list of letters could be moved into partial to be used multiple times (on top and bottom for example).

]]>
By: lanetrain http://www.glennfu.com/2008/06/24/rails-pagination-by-letters-not-numbers-like-will_paginate/comment-page-1/#comment-74 lanetrain Sun, 04 Jan 2009 21:19:59 +0000 http://www.glennfu.com/?p=36#comment-74 Oops - you should do this in the view too! Oops – you should do this in the view too!

]]>
By: lanetrain http://www.glennfu.com/2008/06/24/rails-pagination-by-letters-not-numbers-like-will_paginate/comment-page-1/#comment-73 lanetrain Sun, 04 Jan 2009 21:19:03 +0000 http://www.glennfu.com/?p=36#comment-73 Hi, May be a bit late but you're initiating @letter but not using it in the controller. This causes no data if params[:letter] isn't set. I changed the conditional statements to use @letter instead of params[:letter] Thanks Hi,

May be a bit late but you’re initiating @letter but not using it in the controller. This causes no data if params[:letter] isn’t set. I changed the conditional statements to use @letter instead of params[:letter]

Thanks

]]>
By: Lajuana http://www.glennfu.com/2008/06/24/rails-pagination-by-letters-not-numbers-like-will_paginate/comment-page-1/#comment-71 Lajuana Mon, 27 Oct 2008 18:01:34 +0000 http://www.glennfu.com/?p=36#comment-71 Well said. Well said.

]]>
By: Glenn Ford http://www.glennfu.com/2008/06/24/rails-pagination-by-letters-not-numbers-like-will_paginate/comment-page-1/#comment-62 Glenn Ford Thu, 04 Sep 2008 14:44:22 +0000 http://www.glennfu.com/?p=36#comment-62 Yes it should be the current page. The goal is to reload the same page, only with a different 'letter' parameter so that your controller action can load the new data. Yes it should be the current page. The goal is to reload the same page, only with a different ‘letter’ parameter so that your controller action can load the new data.

]]>
By: Nathan http://www.glennfu.com/2008/06/24/rails-pagination-by-letters-not-numbers-like-will_paginate/comment-page-1/#comment-61 Nathan Thu, 04 Sep 2008 14:41:22 +0000 http://www.glennfu.com/?p=36#comment-61 I just have the following: /:controller/:action/:id /:controller/:action/:id.:format what exactly am I linking to? Is it the current page? I just have the following:

/:controller/:action/:id
/:controller/:action/:id.:format

what exactly am I linking to? Is it the current page?

]]>
By: Glenn Ford http://www.glennfu.com/2008/06/24/rails-pagination-by-letters-not-numbers-like-will_paginate/comment-page-1/#comment-60 Glenn Ford Wed, 03 Sep 2008 22:19:56 +0000 http://www.glennfu.com/?p=36#comment-60 @Nathan: staff_games_path a particular path in my application. Run 'rake routes' in your console to see what paths exist in your own application. @Nathan: staff_games_path a particular path in my application. Run ‘rake routes’ in your console to see what paths exist in your own application.

]]>
By: Nathan http://www.glennfu.com/2008/06/24/rails-pagination-by-letters-not-numbers-like-will_paginate/comment-page-1/#comment-59 Nathan Wed, 03 Sep 2008 21:44:06 +0000 http://www.glennfu.com/?p=36#comment-59 sorry newbie question.... I get an error when using staff_games_path....why is that? and how do I fix it. sorry newbie question…. I get an error when using staff_games_path….why is that? and how do I fix it.

]]>
By: Phil Misiowiec http://www.glennfu.com/2008/06/24/rails-pagination-by-letters-not-numbers-like-will_paginate/comment-page-1/#comment-58 Phil Misiowiec Thu, 28 Aug 2008 21:19:45 +0000 http://www.glennfu.com/?p=36#comment-58 You bet! Above code can be simpliflied it a bit by using the Rails 'first' string extension. @letter_options_list = Model.active.collect!{ |c| c.title.first.upcase }.uniq!.sort! You bet! Above code can be simpliflied it a bit by using the Rails ‘first’ string extension.

@letter_options_list = Model.active.collect!{ |c| c.title.first.upcase }.uniq!.sort!

]]>