簡體   English   中英

will_paginate-協會

[英]will_paginate - Associations

我正在嘗試對協會進行分頁,但我缺少一些東西。

這是我需要使用.paginate(:page => params[:page], :per_page => 25)進行分頁的地方。 如果我理解正確,則必須在控制器中進行變量設置才能獲取城鎮?

<% @alliance.players.each do |p| %>
  <% p.towns.each do |t| %>
    ...
  <% end %>
<% end %>

我在說什么:

Alliance ->
  Players ->
    Towns <--

基本上,我停留在如何在第二級循環中對協會進行分頁。

也許有更好的方法可以做到這一點。


關聯:

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

我已經嘗試閱讀了很多,但是還沒有找到任何解決方案。

我試圖在Controller中創建一個變量:

@alliance_towns = @alliance.players.towns.order("rank ASC").paginate(:page => params[:page], :per_page => 25)

所以我可以打電話給@alliance_towns.each do {}但是在此過程中

undefined method `towns' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Player:0x007f9268d91348>

我想念什么?

您應該使用join 像這樣:

@alliance_towns = Town.joins(:player).where('players.alliance_id = ?', params[:id]).order('rank ASC').paginate(:page => params[:page], :per_page => 25)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM