2016-04-07 15 views
2

tarafından böyle Ödeme, Fatura ve İşlem modelleri kurulum var: müşterilerimden olarakRaylar tür sorgu tarihe

# transaction.rb 
class Transaction < ActiveRecord::Base 
    belongs_to :customer 
    belongs_to :transactionable, polymorphic: true 
end 

# payment.rb 
class Payment < ActiveRecord::Base 
    belongs_to :customer 
    has_many :transactions, as: :transactionable 
end 

# invoice.rb 
class Invoice < ActiveRecord::Base 
    belongs_to :customer 
    has_many :transactions, as: :transactionable 
end 

şablonu göstermek içerisinde, istenen müşteriye ait tüm işlemleri buluyorum. İşlemleri ait oldukları Fatura veya Ödeme tarihine göre sıralamak istiyorum. Bunu başarmanın en iyi yolu nedir? Bir işlem daima ödeme veya fatura yanında oluşturulursa

class CustomersController < ApplicationController 
    def show 
    @customer = Customer.find(params[:id]) 

    # I want the transactions sorted by the date of the Invoice or Payment they belong to 
    @transactions = Transaction.where(customer: @customer) 
    end 
end 

cevap

1

, sen işlemler masaya created_at damgası kullanabilirsiniz. Bununla birlikte, eğer işlem modelinin, hangi modele ait olabileceğini bilmesini engellemek için iyi bir seçenek, işlem tablosunda başka bir datetime sütunu oluşturmaktır ve bu datetime ne olursa olsun en güncel olanı ile güncellenmelidir. ilişkili nesne

+0

, ilgili 'işlem Zamanı 'üzerine kaydedilmelidir "İşlem", "Fatura" veya "Ödeme" nin herhangi bir zamanından ayrıdır. – br3nt

+0

Bu işe yarardı, ancak işlemleri, ödeme tutarı veya fatura gibi diğer değerlere göre sıralamak istediğimde, bu sütunları işlem tablosuna da eklemem gerekecek. Bu gereksiz görünüyor. – James

2

Bu kapsamlar ile mümkün olabilir.

Sen böyle bir şey yapmak mümkün olmalıdır aşağıdadır:

transactions = Transaction.all 
transactions = transactions.order_by_payment_date 
transactions = transactions.order_by_invoice_date 
transactions = transactions.includes_transactionable 

transactions.each do |transaction| 
    # do what you want with the transaction and transactable 
end 

Umarım bu kod sadece çalışır:

Bu çözüm ile anlaşmak
class Transaction < ActiveRecord::Base 
    belongs_to :customer 
    belongs_to :transactionable, polymorphic: true 

    scope :includes_transactionable, -> { includes(:transactionable) } 

    scope :order_by_payment_date, -> { 
    # This may or may not work, you may need to specify a has_many 
    # relationship to Payment, or do more to get the join to work 
    joins(Payment.arel_table).merge(Payment.descending) 
    } 

    scope :order_by_invoice_date, -> { 
    # This may or may not work, you may need to specify a has_many 
    # relationship to Invoice, or do more to get the join to work 
    joins(Invoice.arel_table).merge(Invoice.descending) 
    } 
end 

class Payment < ActiveRecord::Base 
    belongs_to :customer 
    has_many :transactions, as: :transactionable 

    scope :descending, -> { order(arel_table[:payment_date].desc) } 
end 

class Invoice < ActiveRecord::Base 
    belongs_to :customer 
    has_many :transactions, as: :transactionable 

    scope :descending, -> { order(arel_table[:invoice_date].desc) } 
end