Reputation: 7941
I'm trying to paginate an Association but i'm missing something.
This is where i need the pagination with .paginate(:page => params[:page], :per_page => 25)
. If i understood correctly i have to make a variable in my controller to fetch the towns ?
<% @alliance.players.each do |p| %>
<% p.towns.each do |t| %>
...
<% end %>
<% end %>
What i'm calling:
Alliance ->
Players ->
Towns <--
Basically i'm stuck on how to paginate the Association in a 2nd level loop.
Maybe there is a better way of doing this.
The associations:
class Alliance < ActiveRecord::Base
# Primary Key
self.primary_key = 'grepo_id'
# Associations
has_many :players
end
class Player < ActiveRecord::Base
# Primary Key
self.primary_key = 'grepo_id'
# Associations
has_many :towns
belongs_to :alliance
end
class Town < ActiveRecord::Base
# Primary Key
self.primary_key = 'grepo_id'
# Associations
belongs_to :player, :foreign_key => :player_id
end
I've tried and read a lot but haven't found any solution.
I tried to make a variable in my Controller:
@alliance_towns = @alliance.players.towns.order("rank ASC").paginate(:page => params[:page], :per_page => 25)
so i can call @alliance_towns.each do {}
but on this i'm getting
undefined method `towns' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Player:0x007f9268d91348>
What am i missing ?
Upvotes: 2
Views: 1238