I have this strange behavior and I don’t undertand why. Here is the thing:
I have this record in my payment model:
1.9.3p286 :019 > u.payment.last
Payment Load (0.3ms) SELECT "payments".* FROM "payments" WHERE "payments"."user_id" = 10
=> [#<Payment id: 37, bank_name: "Mercantil", plan: "Plan Uno", date: "2012-12-25", reference_number: "3452435", coupon: "", user_id: 10, created_at: "2012-12-25 21:56:12", updated_at: "2012-12-25 21:58:31", active_until: "2013-01-24">]
As you can see, I have one record for my user: 10.
If I try to get the same information in my controller I don’t get any exception, but
@user.payment.last.active_until
is empty, the same in the view.
For example if I try this in my view:
<%= @user.payment.last.active_until %>
I didn’t get anything, is blank.
If I try this
<%= @user.payment %>
I got the same as my console
[#<Payment id: 37, bank_name: "Mercantil", plan: "Plan Uno", date: "2012-12-25", reference_number: "3452435", coupon: "", user_id: 10, created_at: "2012-12-25 21:56:12", updated_at: "2012-12-25 21:58:31", active_until: "2013-01-24">, #<Payment id: nil, bank_name: nil, plan: nil, date: nil, reference_number: nil, coupon: nil, user_id: 10, created_at: nil, updated_at: nil, active_until: nil>]
I really don’t understand what happend here. Any help please.
Thanks in advance.
PD: The user could has many payments, but I need just the last.
It’s not the same as in your console. In the console you get have one record, but in the view you get two – the record that you see in the console, and another blank one.
I’m guessing the blank one is the one that you are using for the
formtag helper for the form used to create a newPaymentrecord, but that’s also the one thatlastreturns. Either take the one before last, or filter for saved records before you calllast.