按照在Postgresql和Ruby On Rails中创建的顺序显示连接记录数据

时间:2014-06-23 07:34:45

标签: ruby-on-rails postgresql postgresql-9.1

我在postgresql数据库中有三个表:

  1. 成员

    • ID
    • 如first_name
    • 姓氏
    • created_at
  2. 付款(如借记卡)

    • ID
    • member_id
    • 金额
    • created_at
  3. 费用(如信用证)

    • ID
    • member_id
    • 金额
    • created_at
  4. 我想在单个视图中显示他们的结算声明: 我使用了以下查询:

    select * from members as t1 left join payments as t2 on (t1.id=t2.member_id) left join fees as t3 on (t1.id=t3.member_id) 
    

    它显示了以下结果:

    id | Amount | created_at | Type
    1   100      25-12-2013   Fee
    2   100      29-12-2013   Fee
    3   100      25-12-2013   Payment
    4   100      23-12-2013   Payment
    

    但问题是,它显示了第一笔费用记录,然后付款,而我需要它应该是由created_at订购,它应该如下所示:

    id | Amount | created_at | Type
    1   100      23-12-2013   Payment
    2   100      25-12-2013   Fee
    3   100      25-12-2013   Payment
    4   100      29-12-2013   Fee
    

    以下是模型的详细信息:

    class Fee < ActiveRecord::Base
    
      # Setup accessible (or protected) attributes for your model
      attr_accessible :member_id,:fee_type,:amount,:reason,:details,:date
    
      # Setup relations with relative models
      belongs_to :member
    
      # Validates rule here
      validates_numericality_of :member_id,:fee_type,:amount
    
      validates :reason,
        :format => {:with => /^[-a-zA-Z0-9_ ]+$/ },
        :length => {:maximum => 255},
        :presence =>true
      validates :details,
        :format => {:with => /^[-a-zA-Z0-9_ ]+$/ },
        :length => {:maximum => 255},
        :allow_blank =>true
      validates :amount,
        :presence => true
      validates :member_id,
        :presence => true
      validates :date,
        :presence => true
    
      # Search start here
      self.per_page = 10
    
    end
    
    class Payment < ActiveRecord::Base
    
      # Setup accessible (or protected) attributes for your model
      attr_accessible :member_id,:payment_category_id,:payment_type_id,:payment_date,:amount,:notes,:is_send_email,:last_sent,:date_deposite
      # Setup relations with relative models 
      belongs_to :payment_type
      belongs_to :payment_category
      belongs_to :member
    
      # Validates rule here
      validates_numericality_of :member_id,:payment_category_id,:payment_type_id,:amount
    
      validates :notes,
        :format => {:with => /^[-a-zA-Z0-9_ ]+$/ },
        :length => {:maximum => 255},
        :allow_blank =>true
      validates :amount,
        :presence => true,
        :length => {:maximum => 15}
      validates :member_id,
        :presence => true
      validates :payment_date,
        :presence => true
    
      # Pagiination start here
      self.per_page = 10
    
    end
    
    class Member < ActiveRecord::Base
    
      has_many :payment, dependent: :destroy, -> { order :created_at }
      has_many :fee, dependent: :destroy, -> { order :created_at }
      has_many :events, :through => :participants
    
    end
    

2 个答案:

答案 0 :(得分:2)

如果我是你,我会像这样使用ActiveRecord Associations

#app/models/member.rb
Class Member < ActiveRecord::Base
   has_many :payments, -> { order :created_at }
   has_many :fees, -> { order :created_at }
end

#app/models/payment.rb
Class Payment < ActiveRecord::Base
   belongs_to :member
end

#app/models/fee.rb
Class Fee < ActiveRecord::Base
   belongs_to :member
end

@member = Member.find 1
@member.payments #-> should be ordered by created_at

答案 1 :(得分:1)

根据您提取的记录数量,您还可以在将数据库拉出数据库后进行ruby排序,例如

transactions = member.payments + member.fees
transactions = transactions.flatten.sort_by(&:created_at)