0

I'm using kaminari for pagination. Then I have 3 models such as User, Community, and Uniquecode.

I'm facing the problem, in which the number of shown records varie(change) at each params[:page].

In addition, if I put <% @uniquecode_count %>, it returns '1'... Since there are 3 related records, it has to be '3', though.

This is really weird. In my case. I have related 3 Uniquecode records.
As I did put paginates_per 1 in Uniquecode model, it should show only 1 record at each page. However the result is

  • At Page. 1 It shows 3 records
  • At Page. 2 It shows 2 records
  • At Page. 3 It shows 1 record

Why does do this? I never seen Kaminari doing this.

Can anyone help me to solve this?

I defined association like this

User has_many :communities
User has_many :uniquecodes

Community belongs_to :user
Community has_many :uniquecodes

Uniquecode belongs_to :user
Uniquecode belongs_to :community

uniquecode model

paginates_per 1

controller

@user = User.find(params[:id])
@uniquecodes = @user.uniquecodes.page(params[:page])
@uniquecodes_count = @uniquecodes.count

view

<%= paginate @uniquecodes, :window => 4 %>
<% @uniquecodes.recent.each do |uniquecode| %>   
    <%= render 'uniquecodes/uniquecode', :uniquecode => uniquecode %>
<% end %>

<% @uniquecode_count %>  => this shows '1'. It has to show '3' though.
MKK
  • 2,713
  • 5
  • 31
  • 51

2 Answers2

1

maybe you should set a default order for your uniquecodes..?

BvuRVKyUVlViVIc7
  • 11,641
  • 9
  • 59
  • 111
  • I'm not using any order as you see here. all I have `scope :recent, lambda { |n = 10| includes(:user).where('users.deleted_at' => nil).order("users.last_active_at DESC").limit(n) }` in my uniquecode model. But I'm not using that here. – MKK Jan 24 '13 at 21:17
  • scope :recent, lambda { |n = 10| includes(:user).where('users.deleted_at' => nil).order("users.last_active_at DESC").limit(n).order("uniquecodes.created_at desc") } – BvuRVKyUVlViVIc7 Jan 24 '13 at 23:05
  • Thanks but the same result:( – MKK Jan 24 '13 at 23:27
1

Replace

@uniquecodes = @user.uniquecodes.page(params[:page])

by

@uniquecodes = Kaminari.paginate_array(@user.uniquecodes).page(params[:page])
Marcel Hebing
  • 3,072
  • 1
  • 19
  • 22