Johnny Cash
Johnny Cash

Reputation: 5147

rails records push

Controller.rb

@payments = PaymentDetail.joins(:project)

in view file @payments.count is equal to 550

When I change my controller like this

@payments = PaymentDetail.joins(:project)
@payment_errors = PaymentError.joins(:project)
@payment_errors.each {|payment_error| @payments << payment_error}

Still in view file @payments.count is equal to 550 while it have to be (550+@payment_errors.count)

Why I can't push @payment_error records into the @payments ?

Upvotes: 0

Views: 38

Answers (2)

Rahul Tapali
Rahul Tapali

Reputation: 10137

You are trying to add PaymentError data into PaymentDetail table which is wrong. If you need it in array use to_a.

You can do like this:

 @payments = PaymentDetail.joins(:project).to_a + PaymentError.joins(:project).to_a

To use will_paginate for arrays. Add this line in your controller:

 `require 'will_paginate/array'`

Upvotes: 1

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230481

It's because of lazy loading.

PaymentDetail.joins(:project)

This does not load payments immediately. Instead payments are loaded as soon as you start accessing them (in an each loop, for example). You can force loading by casting result to array. Try this:

@payments = PaymentDetail.joins(:project).to_a
@payment_errors = PaymentError.joins(:project).to_a
@payments += @payment_errors

Or, if you don't need @payment_errors as separate var:

@payments = PaymentDetail.joins(:project).to_a + PaymentError.joins(:project).to_a

Upvotes: 0

Related Questions