ruby - Paginate many has_many belongs_to relationships in Rails with will_paginate -
a user has_many potatoes. potatoes belong_to user. potato has_many quirks. quirks belong_to potatoes.
i want display potatoes , associated quirks in users show , paginate using will_paginate gem below.
potato_names -> sweet potato russet potato mystery potato quirk_name -> shiny rough purple quirk_name -> big brown orange quirk_name -> old medium blue
my users controller pre-pagination
userscontroller def show @user = user.find(params[:id]) @potatoes = @user.potatoes @quirks = quirk.where(potato: @potatoes) end
i can paginate potatoes changing userscontroller -
@potatoes = @user.potatoes.paginate(page: params[:page])
and users - show.html.erb
<%= @potatoes.potato_name %> <%= will_paginate @potatoes %>
but how can quirks paginate potatoes when click next potatoes , quirks associated next set? getting potatoes paginate , quirks show itself, why didn't display quirks previously. wasn't understanding .pagination call enough.
i can call :param_name specify parameter name page number, , make sure it's working appropriately.
i can do
userscontroller def show @user = user.find(params[:id]) @potatoes = @user.potatoes.paginate(page: params[:potatoes_page]) @quirks = quirk.where(potato: @potatoes).paginate(page: params[:quirks_page]) end
show.html.erb
<%= render @potatoes %> <%= render @quirks %> <%= will_paginate @potatoes, :params_name => 'potatoes_page' %> <%= will_paginate @quirks, :params_name => 'quirks_page' %>
_potato.html.erb
<%= potato.potato_name %>
_quirk.html.erb
<%= quirk.quirk_name %>
this should give me potatoes , quirks paging separately... when next page potatoes, potatoes paging , quirks disappearing. when next page quirks page, potatoes stays same , quirks changes.
i believe above error because i'm calling pagination in potatoes query, using potatoes query call pagination again.
the overall should be:
userscontroller def show @user = user.find(params[:id]) @potatoes = @user.potatoes.paginate(page: params[:page]) @quirks = quirk.where(potato: @user.potatoes).paginate(page: params[:page]) end
show.html.erb
<%= render @potatoes %> <%= render @quirks %> <%= will_paginate @potatoes, :params_name => 'page' %>
_potato.html.erb
<%= potato.potato_name %>
_quirk.html.erb
<%= quirk.quirk_name %>
the error in calling pagination on pagination @potatoes , @quirks in userscontroller. additionally changed :params_name default page both models change @ same time. change them separately, use potatoes_page , quirks_page shown in question. removed
<%= will_paginate @quirks %>
because getting 2 next bars. if changing @potatoes , @quirks separately, need include it.