0

I've 2 classes with a has_and_belongs_to_many relation.

When I try to destroy an object I get:

> undefined method `name' for nil:NilClass error.

I'm using Ruby 2.2.2. The same code works fine with Ruby 2.1.2.

My controller code:

@cart = Cart.find(1)
@cart.temp_orders.find(4).destroy

My models:

class Cart < ActiveRecord::Base
  has_many :temp_orders
end

class TempOrder < ActiveRecord::Base
  belongs_to :cart
  has_and_belongs_to_many :kids, join_table: :kid_temp_orders
end

class Kid < ActiveRecord::Base
  has_and_belongs_to_many :temp_orders
end

Stack trace:

> NoMethodError (undefined method `name' for nil:NilClass):   app/controllers/carts_controller.rb:50:in `destroy'
>  Rendered /home/dell/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.8ms)
>  Rendered /home/dell/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (29.3ms)
>  Rendered /home/dell/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (10.7ms
>  Rendered /home/dell/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (123.5ms
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
Savitha
  • 41
  • 1
  • 3
    Can you post the models, and the stacktrace – j-dexx Aug 18 '15 at 12:40
  • Except for the fact that you're calling the 'name' method on something which is expected, but was not retrieved (so returns `nil`, which is `Nil::NilClass`). We can't tell what has gone wrong here. The stack trace for that error should tell you where the 'name' method was called, but we really need to see where the thing it's being called on comes from. Probably a find, new or the collection. The fact it's on deletion means this is probably on a callback, are you logging a line which has `thing.name` in it on deletion? – AJFaraday Aug 18 '15 at 12:47
  • There is no field in any table with attribute - "name" – Savitha Aug 18 '15 at 13:10
  • You would not normally hardcode ids into your controller code: usually you would get data like `id` from `params`. What's going on with that? – Max Williams Aug 18 '15 at 13:23
  • What's on the line which the error refers to: `app/controllers/carts_controller.rb:50:in `destroy'`? – Max Williams Aug 18 '15 at 13:25
  • Yes, I do have params[:id] which is getting passed to the controller correct. In line 50 I have - @cart.temp_orders.find(params[:temp_order_id]).destroy – Savitha Aug 18 '15 at 13:33

2 Answers2

4

OP indeed can't post any other data for this error. I think OP doesn't deserve those down votes in this case.

This is an issue with ActiveRecord and Ruby 2.2

You can fix it by switching the ruby version from ruby-2.2.0 to ruby-2.1.2/ruby-2.1.3 or rails version from 4.0.0 to 4.1.2

Check this SO link

Community
  • 1
  • 1
Vamsi Krishna
  • 3,742
  • 4
  • 20
  • 45
  • 1
    @RafaelOliveira Many people are facing this issue after upgrade. Need to find out the exact version pair where this works. May be for now we can take that as Rails - 4.0.13 and Ruby - 2.2.3 :D. Thanks for the info by the way... – Vamsi Krishna Sep 10 '15 at 05:40
0

upgrading from:

ruby '2.3.0'
gem 'rails', '4.1.1'

to:

ruby '2.3.0'    
gem 'rails', '4.1.16'

did the trick for me

Riley Guerin
  • 305
  • 3
  • 11