Jean
2012-Dec-26 00:29 UTC
has value in a console but it''s nil in my controller and my view?
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/eO7e0lKkhc8J. For more options, visit https://groups.google.com/groups/opt_out.
William Herry
2012-Dec-26 06:55 UTC
Re: has value in a console but it''s nil in my controller and my view?
it seem some code create one nil record, maybe in controller notice the payment id in console is 37, and in view, you get that record but not the last one On Wed, Dec 26, 2012 at 8:29 AM, Jean <josorioe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/eO7e0lKkhc8J. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Matt Jones
2012-Dec-26 23:29 UTC
Re: has value in a console but it''s nil in my controller and my view?
On Tuesday, 25 December 2012 19:29:59 UTC-5, Jean wrote:> > 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">] > >A quick style note - if a User record has many Payment objects, the naming convention is to use the plural form - so the association in User would be ''has_many :payments'', not ''has_many :payment''. Not related to the problem here, but fixing it will make the code easier to read for other developers. 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>] > >Actually you *didn''t* get the same as in console - there''s that extra record with all fields set to nil. Does your controller call ''@user.payment.build'' someplace? --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/GrVhRbzvz-4J. For more options, visit https://groups.google.com/groups/opt_out.