2016-04-04 5 views
4

私のアプリケーションでは、多くの支払いと請求書を持つ顧客モデルがあります。顧客にRailsはクエリー結果を組み合わせたwill_paginateを使用します

# customer.rb 
class Customer < ActiveRecord::Base 
    has_many :payments 
    has_many :invoices 
end 

# payment.rb 
class Payment < ActiveRecord::Base 
    belongs_to :customer 
end 

# invoice.rb 
class Invoice < ActiveRecord::Base 
    belongs_to :customer 
end 

私はすべての請求書と支払を組み合わせ、@transactionsインスタンス変数に格納していたテンプレートを示しています。

class CustomersController < ApplicationController 
    def show 
    @customer = Customer.find(params[:id]) 
    payments = Payment.where(customer: @customer) 
    invoices = Invoice.where(customer: @customer) 
    @transactions = payments + invoices 
    end 

will_paginateを使用して@トランザクションにページを設定したいとします。これを行うと機能しません:

@transactions.paginate(page: params[:page]) 

これを実行するにはどうすればよいでしょうか?

+0

この回答はあなたの問題を解決しますhttp://stackoverflow.com/questions/4352895/ruby-on-rails-will-paginate-an-array – Thorin

答えて

4

最良の方法は、多型関連を持つトランザクションの第3のテーブルをトランザクション可能なものとして作成することです。また、トランザクションをページングします。

http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

まず、両方のページ番号を付けたりのpaginate配列を使用するのはよくないです

class Transaction < ActiveRecord::Base 
    belongs_to :transactionable, polymorphic: true 
end 

class Invoice < ActiveRecord::Base 
    has_many :transactions, as: :transactionable 
end 

class Payment < ActiveRecord::Base 
    has_many :transactions, as: :transactionable 
end 

その他の方法でポリモーフィックな関連はこちらをご覧ください。

+0

私はレールが初めてです。あなたは私がこれを行う方法を学ぶことができるリソースを提供できますか? – James

+0

http://guides.rubyonrails.org/association_basics.html#polymorphic-associations –

関連する問題