Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoMethodError (undefined method `paper_trail_enabled_for_model?' for NilClass:Class) #509

Closed
westonganger opened this issue Mar 24, 2015 · 19 comments

Comments

Projects
None yet
3 participants
@westonganger
Copy link
Contributor

commented Mar 24, 2015

I am getting the following error(I am using master/4.0.0beta3):

 SQL (0.2ms)  INSERT INTO `versions` (`created_at`, `event`, `item_id`, `item_type`, `object`, `transaction_id`, `whodunnit`) VALUES ('2015-03-24 16:44:46', 'create', 54, 'NetworkLink', NULL, NULL, 6)
   (0.2ms)  UPDATE `versions` SET `transaction_id` = 160 WHERE `versions`.`id` = 160
   (83.0ms)  ROLLBACK
  PaperTrail::Version Load (0.4ms)  SELECT `versions`.* FROM `versions` WHERE `versions`.`item_type` = 'Authorization' AND `versions`.`item_id` IN (5) ORDER BY `versions`.`created_at` ASC, `versions`.`id` ASC
  PaperTrail::Version Load (0.2ms)  SELECT `versions`.* FROM `versions` WHERE `versions`.`item_type` = 'NetworkLink' AND `versions`.`item_id` IN (54) ORDER BY `versions`.`created_at` ASC, `versions`.`id` ASC
Completed 500 Internal Server Error in 181.6ms

NoMethodError (undefined method `paper_trail_enabled_for_model?' for NilClass:Class):

My nested form is for an Authorization which accepts nested attributes for network_links

My Models:

class Authorization < ActiveRecord::Base
  has_paper_trail on: [:update, :delete], only: [:revision]
  has_many :network_links

  accepts_nested_attributes_for :network_links, reject_if: :all_blank, allow_destroy: true
end
class NetworkLink < ActiveRecord::Base
  has_paper_trail
  belongs_to :authorization
end
@batter

This comment has been minimized.

Copy link
Collaborator

commented Mar 24, 2015

What is the call you are making that is triggering this error? Just an update to an Authorization model instance?

@westonganger

This comment has been minimized.

Copy link
Contributor Author

commented Mar 24, 2015

It the call to version the first network link which is causing it

@westonganger

This comment has been minimized.

Copy link
Contributor Author

commented Mar 26, 2015

Any idea of what is causing this?

@batter

This comment has been minimized.

Copy link
Collaborator

commented Mar 26, 2015

No, would be helpful to see a full stack trace at least or a dummy app / reproducible test case

@westonganger

This comment has been minimized.

Copy link
Contributor Author

commented Mar 26, 2015

vendor/cache/paper_trail-090f8d047f76/lib/paper_trail/has_paper_trail.rb:382:in `block in save_associations'
vendor/cache/paper_trail-090f8d047f76/lib/paper_trail/has_paper_trail.rb:375:in `each'
vendor/cache/paper_trail-090f8d047f76/lib/paper_trail/has_paper_trail.rb:375:in `save_associations'
vendor/cache/paper_trail-090f8d047f76/lib/paper_trail/has_paper_trail.rb:322:in `record_update'
activesupport (3.2.21) lib/active_support/callbacks.rb:424:in `_run__1749282600829534398__update__4537807599279933803__callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.21) lib/active_support/callbacks.rb:385:in `_run_update_callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.2.21) lib/active_record/callbacks.rb:272:in `update'
activerecord (3.2.21) lib/active_record/persistence.rb:348:in `create_or_update'
activerecord (3.2.21) lib/active_record/callbacks.rb:264:in `block in create_or_update'
activesupport (3.2.21) lib/active_support/callbacks.rb:513:in `_run__1749282600829534398__save__4537807599279933803__callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.21) lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.2.21) lib/active_record/callbacks.rb:264:in `create_or_update'
activerecord (3.2.21) lib/active_record/persistence.rb:84:in `save'
activerecord (3.2.21) lib/active_record/validations.rb:50:in `save'
activerecord (3.2.21) lib/active_record/attribute_methods/dirty.rb:22:in `save'
activerecord (3.2.21) lib/active_record/transactions.rb:259:in `block (2 levels) in save'
activerecord (3.2.21) lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
activerecord (3.2.21) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.2.21) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.2.21) lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
activerecord (3.2.21) lib/active_record/transactions.rb:259:in `block in save'
activerecord (3.2.21) lib/active_record/transactions.rb:270:in `rollback_active_record_state!'
activerecord (3.2.21) lib/active_record/transactions.rb:258:in `save'
activerecord (3.2.21) lib/active_record/autosave_association.rb:353:in `block in save_collection_association'
activerecord (3.2.21) lib/active_record/autosave_association.rb:341:in `each'
activerecord (3.2.21) lib/active_record/autosave_association.rb:341:in `save_collection_association'
activerecord (3.2.21) lib/active_record/autosave_association.rb:191:in `block in add_autosave_association_callbacks'
activerecord (3.2.21) lib/active_record/autosave_association.rb:161:in `instance_eval'
activerecord (3.2.21) lib/active_record/autosave_association.rb:161:in `block in define_non_cyclic_method'
activesupport (3.2.21) lib/active_support/callbacks.rb:444:in `_run__3996741292422570851__update__4537807599279933803__callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.21) lib/active_support/callbacks.rb:385:in `_run_update_callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.2.21) lib/active_record/callbacks.rb:272:in `update'
activerecord (3.2.21) lib/active_record/persistence.rb:348:in `create_or_update'
activerecord (3.2.21) lib/active_record/callbacks.rb:264:in `block in create_or_update'
activesupport (3.2.21) lib/active_support/callbacks.rb:458:in `_run__3996741292422570851__save__4537807599279933803__callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.21) lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.2.21) lib/active_record/callbacks.rb:264:in `create_or_update'
activerecord (3.2.21) lib/active_record/persistence.rb:84:in `save'
activerecord (3.2.21) lib/active_record/validations.rb:50:in `save'
activerecord (3.2.21) lib/active_record/attribute_methods/dirty.rb:22:in `save'
activerecord (3.2.21) lib/active_record/transactions.rb:259:in `block (2 levels) in save'
activerecord (3.2.21) lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
activerecord (3.2.21) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.2.21) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.2.21) lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
activerecord (3.2.21) lib/active_record/transactions.rb:259:in `block in save'
activerecord (3.2.21) lib/active_record/transactions.rb:270:in `rollback_active_record_state!'
activerecord (3.2.21) lib/active_record/transactions.rb:258:in `save'
app/controllers/ip_auths/network_links_controller.rb:32:in `block in update_authorization'
actionpack (3.2.21) lib/action_controller/metal/mime_responds.rb:270:in `call'
actionpack (3.2.21) lib/action_controller/metal/mime_responds.rb:270:in `retrieve_collector_from_mimes'
actionpack (3.2.21) lib/action_controller/metal/mime_responds.rb:194:in `respond_to'
app/controllers/ip_auths/network_links_controller.rb:30:in `update_authorization'
actionpack (3.2.21) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.21) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.21) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.21) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.21) lib/active_support/callbacks.rb:492:in `block in _run__889286455359724286__process_action__664445304614141480__callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:215:in `block in _conditional_callback_around_1902'
activesupport (3.2.21) lib/active_support/callbacks.rb:326:in `around'
activesupport (3.2.21) lib/active_support/callbacks.rb:310:in `_callback_around_13'
activesupport (3.2.21) lib/active_support/callbacks.rb:214:in `_conditional_callback_around_1902'
activesupport (3.2.21) lib/active_support/callbacks.rb:414:in `_run__889286455359724286__process_action__664445304614141480__callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.21) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.21) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.21) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.21) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.21) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.21) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.21) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.21) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.21) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.21) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.21) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.21) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.21) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.21) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.21) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.21) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.21) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.21) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.21) lib/action_dispatch/routing/route_set.rb:608:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/meta_request_handler.rb:13: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'
actionpack (3.2.21) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.21) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.21) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.21) lib/active_support/callbacks.rb:405:in `_run__4353282653387427792__call__4537807599279933803__callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.21) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.21) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.21) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
rack-contrib (1.2.0) lib/rack/contrib/response_headers.rb:17:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/headers.rb:16:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.21) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.21) lib/rails/rack/logger.rb:18:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
request_store (1.1.0) lib/request_store/middleware.rb:8:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.21) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.21) lib/action_dispatch/middleware/static.rb:83:in `call'
railties (3.2.21) lib/rails/engine.rb:484:in `call'
railties (3.2.21) lib/rails/application.rb:231:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.21) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
webrick (1.3.1) lib/webrick/httpserver.rb:138:in `service'
webrick (1.3.1) lib/webrick/httpserver.rb:94:in `run'
webrick (1.3.1) lib/webrick/server.rb:191:in `block in start_thread'
@westonganger

This comment has been minimized.

Copy link
Contributor Author

commented Apr 9, 2015

this issue is now occuring on a totally different set of models too.

In both cases the object and transaction_id are NULL. What could be the culprit here

Edit: object should be null as it was a create action, but transaction_id should not

@westonganger

This comment has been minimized.

Copy link
Contributor Author

commented Apr 9, 2015

Okay so in the new issue I have boiled it down to a polymorphic belongs_to.

How does it work in regards to that?

@batter

This comment has been minimized.

Copy link
Collaborator

commented Apr 9, 2015

Currently there is no support for versioning / restoring belongs_to associations, so this seems like it's your issue. Other similar issues: #498, #503.

@westonganger

This comment has been minimized.

Copy link
Contributor Author

commented Apr 9, 2015

I dont need to restore any of the belongs_to associations. In fact im not even attempted to reify.

Basically when it attempts to version any models that has a belongs_to :foo, polymorphic: true in it, it will fail with this error.

@westonganger

This comment has been minimized.

Copy link
Contributor Author

commented Apr 9, 2015

I dont think it should have any issue with the polymorphic as both of those columns are stored in the object itself. This does seem like a bug to me.

This code here does something with the belongs_to (but i cant tell what lol) and there is a section there for polymorphic but I think my error was happening on the assoc_version_args block(lines 379-382)

     #lib/paper_trail/has_paper_trail.rb - Lines 375-395

      # saves associations if the join table for `VersionAssociation` exists
      def save_associations(version)
        return unless PaperTrail.config.track_associations?
        self.class.reflect_on_all_associations(:belongs_to).each do |assoc|
          assoc_version_args = {
              :version_id => version.id,
              :foreign_key_name => assoc.foreign_key
          }

          if assoc.options[:polymorphic]
            associated_record = send(assoc.name)
            if associated_record && associated_record.class.paper_trail_enabled_for_model?
              assoc_version_args.merge!(:foreign_key_id => associated_record.id)
            end
          elsif assoc.klass.paper_trail_enabled_for_model?
            assoc_version_args.merge!(:foreign_key_id => send(assoc.foreign_key))
          end

          PaperTrail::VersionAssociation.create(assoc_version_args) if assoc_version_args.has_key?(:foreign_key_id)
        end
      end
@westonganger

This comment has been minimized.

Copy link
Contributor Author

commented Apr 9, 2015

I have pinpointed exactly why it gives the error, its fine when you have set the attributes of the polymorphic(:foo_id & :foo_type) but if they are nil then it gives errors.

@batter

This comment has been minimized.

Copy link
Collaborator

commented Apr 9, 2015

Shouldn't this catch it?

associated_record = send(assoc.name)
  if associated_record && associated_record.class.paper_trail_enabled_for_model?
@westonganger

This comment has been minimized.

Copy link
Contributor Author

commented Apr 9, 2015

You would think, but im pretty sure its not working

I've changed it to this and im no longer getting the errors and it seems to be working

Would you like me to make a pull-request of that? (I dont really know how to write any tests though)

associated_record = send(assoc.name) if send(assoc.foreign_type)
  if associated_record && associated_record.class.paper_trail_enabled_for_model?
@dstull

This comment has been minimized.

Copy link

commented Apr 16, 2015

I am having the same issue, has anyone patched the master yet?

@batter

This comment has been minimized.

Copy link
Collaborator

commented Apr 16, 2015

@westonganger - Pull requests are always welcome. Definitely submit one if you have something that is working better than current master please!

@dstull

This comment has been minimized.

Copy link

commented Apr 16, 2015

ok - switched to master. different error now but no mention of paper trail in the exact error message. backed down to 3.0.7 and no issues.. of course i initially upgraded to make use of version associations :(

attempted same monkey patch as westonganger to no avail.

seems it is barfing on trying to handle a belongs_to association

@batter

This comment has been minimized.

Copy link
Collaborator

commented Apr 16, 2015

@dstull - Can you try fd40fcc?

@dstull

This comment has been minimized.

Copy link

commented Apr 16, 2015

@batter i commented out paper_trail on my model that was having the issue on belongs_to and it is fine. I believe my issue is that sometimes i have nil for that belongs_to relationship - similar to the polymorphic issue.

will test a bit more and fork/pull if works out, but i don't think fd40fcc would address this since in the polymorphic block

@dstull

This comment has been minimized.

Copy link

commented Apr 16, 2015

@batter - seems my issue was due to a bad belongs_to association on one of my models. guess it would help if i wrote tests...at least that would tell me if i forgot removing half of an association definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.