Limit total messages displayed in the inbox - ruby-on-rails

I can't seem to find the correct way to set a maximum number of messages to display in a users inbox without it disfiguring the pagination. I'm trying to make it so only the last 100 inbox messages are displayed from newest to oldest.
messages_controller.rb
class MessagesController < ApplicationController
def index
#messages = current_user.received_messages.paginate(:page => params[:page], :per_page => 15, :order => 'created_at DESC', )
end
using the will_paginate gem
<%= will_paginate #messages %>

def index
#messages = current_user.received_messages.paginate(:page => params[:page], :per_page => 15).order('created_at DESC').limit(100)
end
or try with
def index
#records = current_user.received_messages.order('created_at DESC').limit(100)
#messages = #records.paginate(:page => params[:page], :per_page => 15)
end
Hope this will work

But it is a good practice to first implement the active_records conditions, than the pagination's.
def index
#messages = current_user.received_messages.order(:created_at).reverse_order.limit(100).paginate(:page => params[:page], :per_page => 15)
end
Hope it will help.

I have this problem before, My fix was:
require 'will_paginate/array' # To paginate an array instead of ActiveRecord
class MessagesController < ApplicationController
def index
#messages = current_user.received_messages.limit(100).all.paginate(:page => params[:page], :per_page => 15, :order => 'created_at DESC') # I transform the resultset to an array using .all before the paginate
end

Related

Rails update code from rails 2 to rails 3 with will_paginate gem

I'm trying to update the following code on by controller:
#post = Post.paginate_by_user_id(#user.id, :page => params[:page], :per_page => 20, :order => 'created_at DESC')
This was used most likely used with will_paginate plugin for rails 2
paginate_by_user_id or by anything else is no longer used in current will_paginate gem
Does anyone know how, I can pass the #user.id and use Post.paginate and keep the keep same logic as above?
I have everything else need to make this work.
any help would be greatly appreciated.
Thanks,
Try:
#posts = Post.where(:user_id => #user.id).paginate(:page => params[:page], :per_page => 20).order('created_at DESC')
Or if your User model has a has_many relation to posts:
#posts = #user.posts.paginate(:page => params[:page], :per_page => 20).order('created_at DESC')

Issue With Will Paginate and Index Action

In my controller for the index action, I have;
def index
#documents = current_user.documents.all if current_user
end
Anything I add on the end of current_user I get an error. For example, I tried to add will_paginate which is simply adding .paginate(:per_page => 5, :page => params[:page]) to the end of the index action and adding <%= will_paginate #documents %> to the views.
Once I add the .paginate(:per_page => 5, :page => params[:page]) to the end of the index method, like this;
def index
#documents = current_user.documents.all if current_user.paginate(:per_page => 5, :page => params[:page])
end
I get a NoMethodError. Anybody know how to fix this?
Try
def index
#documents = current_user.documents.paginate(:per_page => 5, :page => params[:page]) if current_user
end
You're calling the method on the wrong object, here is the correct one:
#documents = current_user.documents.paginate(:per_page => 5, :page => params[:page]) if current_user

Integrating meta_search gem in index with existing geocoder gem search (rails)

I have already implemented a location based search using geocoder and am having trouble integrating the meta_search gem. I'm trying to integrate meta_search into my object_controller index to allow users to filter and sort search results by an objects :attributes after they have already searched by location.
My object_controller:
def index
if params[:search].present?
#objects = Object.near(params[:search], 50, :order => :distance).paginate(:page => params[:page], :per_page => 9)
else
#objects = Object.paginate(:page => params[:page], :per_page => 9)
end
end
Any idea how best to integrate the #search into the index required by the meta_search gem?
Here is what the meta_search github recommends for the index:
def index
#search = Article.search(params[:search])
#articles = #search.all # load all matching records
# #articles = #search.relation # Retrieve the relation, to lazy-load in view
# #articles = #search.paginate(:page => params[:page]) # Who doesn't love will_paginate?
end
Thanks so much,
Will
I believe both the geocoder and meta_search query methods return an ActiveRecord::Relation therefore you should be able to chain them:
#objects = Object.near(params[:search], 50, :order => :distance).search(params[:search]).relation.paginate(:page => params[:page], :per_page => 9)
or if you need the search object to be separate:
#search = Object.near(params[:search], 50, :order => :distance).search(params[:search])
#objects = #search.relation.paginate(:page => params[:page], :per_page => 9)

How to use will_paginate with a nested resource in Rails?

I'm new to Rails, and I'm having major trouble getting will_paginate to work with a nested resource.
I have two models, Statement and Invoice. will_paginate is working on Statement, but I can't get it to work on Invoice. I know I'd doing something silly, but I can't figure it out and the examples I've found on google won't work for me.
statement.rb
class Statement < ActiveRecord::Base
has_many :invoices
def self.search(search, page)
paginate :per_page => 19, :page => page,
:conditions => ['company like ?', "%#{search}%"],
:order => 'date_due DESC, company, supplier'
end
end
statements_controller.rb <irrelevant code clipped for readability>
def index #taken from the RAILSCAST 51, will_paginate podcast
#statements = Statement.search(params[:search], params[:page])
end
I call this in the view like so, and it works:
<%= will_paginate #statements %>
But I can't figure out how to get it to work for Invoices:
invoice.rb
class Invoice < ActiveRecord::Base
belongs_to :statement
def self.search(search, page)
paginate :per_page => 19, :page => page,
:conditions => ['company like ?', "%#{search}%"],
:order => 'employee'
end
end
invoices_controller.rb
class InvoicesController < ApplicationController
before_filter :find_statement
#TODO I can't get will_paginate to work w a nested resource
def index #taken from the RAILSCAST 51, will_paginate podcast
#invoices = Invoice.search(params[:search], params[:page])
end
def find_statement
#statement_id = params[:statement_id]
return(redirect_to(statements_url)) unless #statement_id
#statement = Statement.find(#statement_id)
end
end
And I try to call it like this:
<%= will_paginate (#invoices) %>
The most common error message, as I play with this, is:
"The #statements variable appears to be empty. Did you forget to pass the collection object for will_paginate?"
I don't have a clue what the problem is, or how to fix it. Thanks for any help and guidance!
Solved -
I moved the invoices pagination into Statement's controller, like this:
def show
#statement = Statement.find(params[:id])
#TODO move the :per_page stuff out to a constant
#invoices = #statement.invoices.paginate :per_page => 10,
:page => params[:page],
:order => 'created_at DESC'
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #statement }
end
end
and call it in the view like this (code trimmed for readability>
<div id="pagination">
<%= will_paginate #invoices %>
</div>
<table>
<%# #statement.invoices.each do |invoice| -
shows all invoices with no pagination,
use #invoices instead%>
<%
#invoices.each do |invoice|
%>

Paginate ordering doesn't work when using find_all_by_completed(false)

In my project model
def incomplete
#clients = current_user.clients.find_all_by_completed(false).paginate
(:page => params[:page], :per_page => 10, :order => 'started_on DESC')
end
For some reason it doesn't order started_on descending. However ordering works in another method
def all
#clients = current_user.clients.paginate(:page => params[:page], :per_page => 25, :order => 'started_on DESC')
end
So I'm assuming using find_all_by_completed is throwing off paginate. I'm using will-paginate btw. Any help?
Try passing the condition explicitly:
#clients = current_user.clients.paginate(
:conditions => {:completed => false},
:page => params[:page], :per_page => 10,
:order => 'started_on DESC')

Resources