2

While trying to delete a product, I am getting this error

NoMethodError in ProductsController#destroy
undefined method `name' for nil:NilClass

Controller:

class ProductsController < ApplicationController
 before_action :set_product, only: [:show, :edit, :update, :destroy]

 def destroy
  @product.destroy <-------error highlights on this line
  respond_to do |format|
   format.html { redirect_to some_path }
   format.json { head :no_content }
  end
 end

 private

 def set_product
  @product = Product.find(params[:id])
 end

Model:

class Product < ActiveRecord::Base

 belongs_to :category
 belongs_to :sub_category

 has_one :item , :dependent => :delete
 has_many :carts, :dependent => :delete_all
 has_many :assets , :dependent => :delete_all

end

View:

<%= link_to image_tag('delete.png'), product, method: :delete, data: { confirm: 'Are you sure?' } %>

Upon clicking 'OK' from the pop-up, it throws above described error.@product exists,I have checked.

In development mode, it runs fine, but in Digital Ocean under development mode, it is throwing this error. Please help me to figure it out.

Application trace:

   app/controllers/products_controller.rb:225:in `destroy'

Full trace:

activerecord (4.0.1) lib/active_record/associations/has_many_association.rb:81:in `cached_counter_attribute_name'
activerecord (4.0.1) lib/active_record/associations/has_many_association.rb:104:in `inverse_updates_counter_cache?'
activerecord (4.0.1) lib/active_record/associations/has_many_association.rb:114:in `delete_records'
activerecord (4.0.1) lib/active_record/associations/collection_association.rb:493:in `remove_records'
activerecord (4.0.1) lib/active_record/associations/collection_association.rb:486:in `block in delete_or_destroy'
activerecord (4.0.1) lib/active_record/associations/collection_association.rb:152:in `block in transaction'
activerecord (4.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:200:in `transaction'
activerecord (4.0.1) lib/active_record/transactions.rb:209:in `transaction'
activerecord (4.0.1) lib/active_record/associations/collection_association.rb:151:in `transaction'
activerecord (4.0.1) lib/active_record/associations/collection_association.rb:486:in `delete_or_destroy'
activerecord (4.0.1) lib/active_record/associations/collection_association.rb:247:in `destroy'
activerecord (4.0.1)  lib/active_record/associations/collection_association.rb:170:in `destroy_all'
activerecord (4.0.1) lib/active_record/associations/has_many_association.rb:26:in `handle_dependency'
 activerecord (4.0.1) lib/active_record/associations/builder/association.rb:97:in `has_many_dependent_for_assets'
 activesupport (4.0.1) lib/active_support/callbacks.rb:397:in `_run__3353912714138952721__destroy__callbacks'
 activesupport (4.0.1) lib/active_support/callbacks.rb:80:in `run_callbacks'
 activerecord (4.0.1) lib/active_record/callbacks.rb:292:in `destroy'
 activerecord (4.0.1) lib/active_record/transactions.rb:265:in `block in destroy'
activerecord (4.0.1) lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
activerecord (4.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
activerecord (4.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
activerecord (4.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
activerecord (4.0.1) lib/active_record/transactions.rb:209:in `transaction'
activerecord (4.0.1) lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
activerecord (4.0.1) lib/active_record/transactions.rb:265:in `destroy'
app/controllers/products_controller.rb:225:in `destroy'
    actionpack (4.0.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
    actionpack (4.0.1) lib/abstract_controller/base.rb:189:in `process_action'
    actionpack (4.0.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
    actionpack (4.0.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
    activesupport (4.0.1) lib/active_support/callbacks.rb:493:in `_run__3348285909198205465__process_action__callbacks'
    activesupport (4.0.1) lib/active_support/callbacks.rb:80:in `run_callbacks'
    actionpack (4.0.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
    actionpack (4.0.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
    actionpack (4.0.1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
    activesupport (4.0.1) lib/active_support/notifications.rb:159:in `block in instrument'
    activesupport (4.0.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    activesupport (4.0.1) lib/active_support/notifications.rb:159:in `instrument'
    actionpack (4.0.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
    actionpack (4.0.1) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
    activerecord (4.0.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
    actionpack (4.0.1) lib/abstract_controller/base.rb:136:in `process'
 actionpack (4.0.1) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.1) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.1) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.1) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.1) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.1) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.1) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.1) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.1) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.1) lib/action_dispatch/routing/route_set.rb:680:in `call'
rack (1.6.4) lib/rack/deflater.rb:35:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.1)   lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.1) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.1) lib/active_support/callbacks.rb:373:in `_run__753779113942060240__call__callbacks'
activesupport (4.0.1) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.1) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.1) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.1) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.0.1) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.0.1) lib/rails/engine.rb:511:in `call'
railties (4.0.1) lib/rails/application.rb:97:in `call'
rack (1.6.4) lib/rack/tempfile_reaper.rb:15:in `call'
rack (1.6.4) lib/rack/lint.rb:49:in `_call'
rack (1.6.4) lib/rack/lint.rb:37:in `call'
rack (1.6.4) lib/rack/showexceptions.rb:24:in `call'
rack (1.6.4) lib/rack/commonlogger.rb:33:in `call'
rack (1.6.4) lib/rack/chunked.rb:54:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
unicorn (5.0.0) lib/unicorn/http_server.rb:562:in `process_client'
unicorn (5.0.0) lib/unicorn/http_server.rb:658:in `worker_loop'
unicorn (5.0.0) lib/unicorn/http_server.rb:508:in `spawn_missing_workers'
unicorn (5.0.0) lib/unicorn/http_server.rb:132:in `start'
unicorn (5.0.0) bin/unicorn:126:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/bin/unicorn:23:in `load'
/usr/local/rvm/gems/ruby-2.2.1/bin/unicorn:23:in `<main>'
/usr/local/rvm/gems/ruby-2.2.1@global/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.2.1@global/bin/ruby_executable_hooks:15:in `<main>'

item.rb

class Item < ActiveRecord::Base
 belongs_to :user
 belongs_to :order
 belongs_to :product
end

cart.rb

class Cart < ActiveRecord::Base
  belongs_to :user
  belongs_to :product
end

asset.rb

class Asset < ActiveRecord::Base
 mount_uploader :asset1, AssetUploader
 mount_uploader :asset2, AssetUploader
 mount_uploader :asset3, AssetUploader
 mount_uploader :asset4, AssetUploader 
end

If I remove :dependent => :delete_all, then this error is gone, but I need to delete the dependents too.

How to get that done?

dips
  • 370
  • 6
  • 17
  • Please add the full error, including backtrace, to the question. Likely a callback on the model. – sevenseacat Nov 16 '15 at 09:43
  • @sevenseacat updated the question with model file and template code. Please have a look – dips Nov 16 '15 at 09:56
  • can you add the full error with backtrace to the question? – sevenseacat Nov 16 '15 at 10:58
  • @sevenseacat added application trace and full trace. I guess you meant the same, if not please let me know what you meant. – dips Nov 16 '15 at 11:17
  • 1
    Could you post the item.rb, cart.rb and asset.rb model definitions, and any callback in those models. I think there must be a counter cache or some other callback that it is blowing up. – rlarcombe Nov 16 '15 at 13:56
  • @rlarcombe updated the post with item.rb, cart.rb and asset.rb model definitions. Please have a look. – dips Nov 17 '15 at 05:29
  • 1
    Are all your migrations up to date on Digital Ocean? Log in to your production database and make sure all your attributes are present. I've had problems where some of the attributes are not present because migrations might be missing. – rii Nov 17 '15 at 18:51
  • I've cross checked all the migrations in development mode as I am running the app in digital ocean under development mode, all are up to date. – dips Nov 18 '15 at 07:20
  • 1
    I know this is an old thread. FWIW, I have encountered this problem as well where deleting a model with associations threw an exception in development environment but not in production environment (Heroku). What I did was to recreate the database (rake db:drop db:create db:migrate db:seed ...) and that fixed the issue. – MichaelZ Feb 16 '16 at 14:46
  • 1
    Possible duplicate of [create with has\_many through association gets NoMethodError (undefined method \`name' for nil:NilClass)](http://stackoverflow.com/questions/23568084/create-with-has-many-through-association-gets-nomethoderror-undefined-method-n) – Bryan Dimas Oct 14 '16 at 16:46

1 Answers1

3

Hey the thing is that you re using the dependent destroy option whith a has_many association, which is cant be done. Thid option is available only for a belongs_to association.

Like a User has many posts, and a Post belongs to User. You want to set dependent destroy on Post, cause you re saying : ** I want this association to depend on User in destroy method ** , by cons when deleting a user, all his posts should be deleted with.

I hope i could have halped clearing things for you

  • 1
    But in this link- http://guides.rubyonrails.org/association_basics.html, dependents are used with has_many. – dips Nov 17 '15 at 06:17
  • Not at all. Hiu can find it in 4.1.2 Options for belongs_to The belongs_to association supports these options: :autosave :class_name :counter_cache :dependent :foreign_key :inverse_of :polymorphic :touch :validate – IKEN Lemjahed Ayoub Nov 17 '15 at 06:22
  • 2
    There is a wrning along with the dependent in belongs_to options- "You should not specify this option on a belongs_to association that is connected with a has_many association on the other class. Doing so can lead to orphaned records in your database." in 4.1.2.4 :dependent in http://guides.rubyonrails.org/association_basics.html – dips Nov 17 '15 at 08:08
  • 4
    @dips Your comment really is a live saver! upgraded Rails to `4.1.2` and all is solved. – Aleks Mar 11 '16 at 19:28