will_paginate - list alphanumerically? - ruby-on-rails-3

Can anyone recommend how to use the will_paginate gem to paginate a list alphanumerically?
There is this plugin:
http://sermoa.wordpress.com/2010/08/25/rails-alphabetical-pagination/
But I was hoping to get my hands on a method with will_paginate
Thanks!
Adam

I just add this in my model:
default_scope order: 'users.name ASC'
Although you could alternatively just do this in your controller:
#users = User.page(params[:page]).order(:name)
Works with kaminari and, I assume, will_paginate

Related

Ruby on Rails 3 (3.1) ActiveModel Associations (tableless nested models)

How to impliment ActiveModel associations (tableless nested models)?
For example:
book has many chapters
With ActiveRecord I would create two models and assosiate them with has_many and belongs_to. But ActiveModel doesn't have such functionality. How can I implement this?
With rails versions >= 2.3.x you can use the activerecord-tableless gem. With that gem you can have associations and validations without a database.
Update
I have been added as author to the gem and I have updated the gem to support newer Rails versions. So now we can have tableless models with associations in Rails versions >= 2.3
You simply can't do it that way. It is not active record.
You can check ActiveModel documentation (and source code) at :
https://github.com/rails/rails/tree/master/activemodel
I guess you have to do it old fashion way, using an array of chapters and a reference to the book in the chapters.
Hope this helps!
You can check out this answer for another way to do it.
class Tableless < ActiveRecord::Base
def self.columns() #columns ||= []; end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end
attr_accessor :id, :name, :value
has_many :stuff_things
has_many :things, :through => :stuff_things
end

Paper_trail and Kaminari problem

I have a ActiveRecord model from paper_trail gem called 'Version'.
I want to create resource with displaying version records.
But Kaminari doesn't add 'page' scope to Version.
Any model has 'page' methods but Version doesn't
Example:
Version.methods.grep /page/
=> []
MyAnyModel.methods.grep /page/
=> [:page, :default_per_page]
this is a bug of kaminari gem -
details are here : https://github.com/amatsuda/kaminari/pull/119
try to use
gem 'kaminari', :git=>"https://github.com/Casecommons/kaminari.git"
try to use this
Kaminari.paginate_array(my_array_object).page(params[:page])
resource:
https://github.com/amatsuda/kaminari

rails3 unscoped within the model gets overriden by default_scope

I have this model
User.rb
default_scope :order => 'users.created_at DESC'
and
scope :ranking, lambda { unscoped { order('users.ranking DESC') }}
and still I get a to_sql that includes ORDER BY users.created_at DESC, users.ranking DESC...
can someone explain why?
I really don't want to have to call unscoped from every controller i'll be using this model in.
Thanks!
As you're discovering, default_scope is often more trouble than it's worth. If you're wanting to stick with it, you could use reorder to ignore the previous order:
scope :ranking, reorder("ranking DESC")
Not sure why #TimPost deleted my answer but I'm using rails 3.0.5 and ruby 1.9.2 for a project and when I used reorder(which works btw) it says this in the log
DEPRECATION WARNING: reorder is deprecated. Please use except(:order).order(...) instead. (called from <class:Item>
So I don't think it is fair my answer was deleted and I got dinged for a crappy response

Having problems with my will_paginate after upgrading to Rails 3

The problem is that I keep getting
NoMethodError (undefined method `paginate' for #<Class:0x1e3dec0>):
activerecord (3.0.3) lib/active_record/base.rb:1008:in `method_missing'
Below is the code snippet I am including and any help in fixing my problem. I am using will_paginate-rails3 at the moment.
def list
#users = User.paginate :per_page => 10, :page => 1
end
in user_controller.rb
and also
<!-<%= link_to 'Previous page', { :page => #users.previous_page } if #users.previous_page %>-->
in my views.html.
Could someone please point me in the right direction?
Did you use the Rails 3 branch of will_paginate? It's here:
https://github.com/mislav/will_paginate/tree/rails3
Specify it in your Gemfile like this:
gem 'will_paginate', '~> 3.0.beta'
then install it like this:
bundle install

How to stop will_paginate from getting every post from the database

I am just playing around with Ruby on Rails 3.0 with a simple message board and found several issues with will_paginate.
The most pressing is that each time a new page is displayed a database query of every single post in the topic is performed.
As you can imagine, if you have a topic with 10,000+ posts this is very slow.
Is there a way to stop this odd behavior?
Show controller:
#posts=#topic.posts
#posts = Post.paginate #posts, :page => params[:page],:order => "post_number"
Model
cattr_reader :per_page
##per_page = 20
view
<%= will_paginate #posts %>
In your controller try:
#posts = Post.paginate_by_topic_id #topic.id, :page => params[:page],:order => "post_number"
Look at the example in the will_paginate docs
Upgrade will_paginate to version 3.0.0. Then:
class Post
self.per_page = 20
end
#topic.posts.page(params[:page]).order("post_number")