Note that there are some explanatory texts on larger screens.

plurals
  1. POActiveRecords not calling the correct SQL statement
    primarykey
    data
    text
    <p>I have a strange problem with ActiveRecords that I've been struggling with for a few days now. I'm still a beginner at rails and was hoping one of the rails gurus on stackoverflow could tell me what I'm doing wrong.</p> <p>I'm using a postgres server and I have a many-to-one relationship between Resources and Comments. I have an action on my controller that I call through ajax to retrieve all comments for a particular resource. I use this method which should return all comments belonging to a particular resource_id.</p> <pre><code>def forresource resource_id = params[:id] @comments = Resource.find_by_id(resource_id).comments respond_to do |format| format.html format.json { render :status=&gt;200, :json=&gt;{:success=&gt;true, :comments =&gt; @comments}} end end </code></pre> <p>However, when run, this method seems to be searching for comments with a comment_id instead.</p> <pre><code>Started POST "/comments/30/forresource" for 127.0.0.1 at 2013-03-04 19:49:05 -0700 Processing by CommentsController#forresource as JSON Parameters: {"id"=&gt;"30"} Comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", "30"]] Completed 404 Not Found in 10ms ActiveRecord::RecordNotFound - Couldn't find Comment with id=30: </code></pre> <p>Can anybody tell me what I'm doing wrong? I've also tried the command.</p> <pre><code>@comments = Comment.where(:resource_id =&gt; resource_id) </code></pre> <p>Here are the abbreviated model relationships</p> <pre><code>class Resource &lt; ActiveRecord::Base has_many :comments end class Comment &lt; ActiveRecord::Base attr_accessible :content, :resource_id, :user_id belongs_to :user belongs_to :resource end </code></pre> <p>Thanks for the help! I've spent the last 3 days beating my head against a wall trying to fix this.</p> <p>Update: Here's the stacktrace from my error:</p> <pre><code>Started POST "/comments/30/forresource" for 127.0.0.1 at 2013-03-04 19:49:05 -0700 Processing by CommentsController#forresource as JSON Parameters: {"id"=&gt;"30"} Comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", "30"]] Completed 404 Not Found in 10ms ActiveRecord::RecordNotFound - Couldn't find Comment with id=30: (gem) activerecord-3.2.11/lib/active_record/relation/finder_methods.rb:341:in `find_one' (gem) activerecord-3.2.11/lib/active_record/relation/finder_methods.rb:312:in `find_with_ids' (gem) activerecord-3.2.11/lib/active_record/relation/finder_methods.rb:107:in `find' (gem) activerecord-3.2.11/lib/active_record/querying.rb:5:in `find' (gem) cancan-1.6.9/lib/cancan/model_adapters/abstract_adapter.rb:20:in `find' (gem) cancan-1.6.9/lib/cancan/controller_resource.rb:114:in `find_resource' (gem) cancan-1.6.9/lib/cancan/controller_resource.rb:68:in `load_resource_instance' (gem) cancan-1.6.9/lib/cancan/controller_resource.rb:32:in `load_resource' (gem) cancan-1.6.9/lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource' (gem) cancan-1.6.9/lib/cancan/controller_resource.rb:10:in `block in add_before_filter' (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:419:in `block in _run__1784171846094811992__process_action__3376091794244018521__callbacks' (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:215:in `block in _conditional_callback_around_709' (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:326:in `around' (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:310:in `_callback_around_137' (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:214:in `_conditional_callback_around_709' (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:414:in `_run__1784171846094811992__process_action__3376091794244018521__callbacks' (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `__run_callback' (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks' (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:81:in `run_callbacks' (gem) actionpack-3.2.11/lib/abstract_controller/callbacks.rb:17:in `process_action' (gem) actionpack-3.2.11/lib/action_controller/metal/rescue.rb:29:in `process_action' (gem) actionpack-3.2.11/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action' (gem) activesupport-3.2.11/lib/active_support/notifications.rb:123:in `block in instrument' (gem) activesupport-3.2.11/lib/active_support/notifications/instrumenter.rb:20:in `instrument' (gem) activesupport-3.2.11/lib/active_support/notifications.rb:123:in `instrument' (gem) actionpack-3.2.11/lib/action_controller/metal/instrumentation.rb:29:in `process_action' (gem) actionpack-3.2.11/lib/action_controller/metal/params_wrapper.rb:207:in `process_action' (gem) activerecord-3.2.11/lib/active_record/railties/controller_runtime.rb:18:in `process_action' (gem) actionpack-3.2.11/lib/abstract_controller/base.rb:121:in `process' (gem) actionpack-3.2.11/lib/abstract_controller/rendering.rb:45:in `process' (gem) actionpack-3.2.11/lib/action_controller/metal.rb:203:in `dispatch' (gem) actionpack-3.2.11/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' (gem) actionpack-3.2.11/lib/action_controller/metal.rb:246:in `block in action' (gem) actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:73:in `dispatch' (gem) actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:36:in `call' (gem) journey-1.0.4/lib/journey/router.rb:68:in `block in call' (gem) journey-1.0.4/lib/journey/router.rb:56:in `call' (gem) actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:601:in `call' (gem) meta_request-0.2.2/lib/meta_request/middlewares/app_request_handler.rb:11:in `call' (gem) rack-contrib-1.1.0/lib/rack/contrib/response_headers.rb:17:in `call' (gem) meta_request-0.2.2/lib/meta_request/middlewares/headers.rb:16:in `call' (gem) meta_request-0.2.2/lib/meta_request/middlewares/meta_request_handler.rb:13:in `call' (gem) omniauth-1.1.3/lib/omniauth/strategy.rb:177:in `call!' (gem) omniauth-1.1.3/lib/omniauth/strategy.rb:157:in `call' (gem) omniauth-1.1.3/lib/omniauth/builder.rb:48:in `call' (gem) warden-1.2.1/lib/warden/manager.rb:35:in `block in call' (gem) warden-1.2.1/lib/warden/manager.rb:34:in `call' (gem) actionpack-3.2.11/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' (gem) rack-1.4.5/lib/rack/etag.rb:23:in `call' (gem) rack-1.4.5/lib/rack/conditionalget.rb:35:in `call' (gem) actionpack-3.2.11/lib/action_dispatch/middleware/head.rb:14:in `call' (gem) actionpack-3.2.11/lib/action_dispatch/middleware/params_parser.rb:21:in `call' (gem) actionpack-3.2.11/lib/action_dispatch/middleware/flash.rb:242:in `call' (gem) rack-1.4.5/lib/rack/session/abstract/id.rb:210:in `context' (gem) rack-1.4.5/lib/rack/session/abstract/id.rb:205:in `call' (gem) actionpack-3.2.11/lib/action_dispatch/middleware/cookies.rb:341:in `call' (gem) activerecord-3.2.11/lib/active_record/query_cache.rb:64:in `call' (gem) activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call' (gem) actionpack-3.2.11/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call' (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `_run__1798271562887333581__call__3889604659297230264__callbacks' (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `__run_callback' (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:385:in `_run_call_callbacks' (gem) activesupport-3.2.11/lib/active_support/callbacks.rb:81:in `run_callbacks' (gem) actionpack-3.2.11/lib/action_dispatch/middleware/callbacks.rb:27:in `call' (gem) actionpack-3.2.11/lib/action_dispatch/middleware/reloader.rb:65:in `call' (gem) actionpack-3.2.11/lib/action_dispatch/middleware/remote_ip.rb:31:in `call' (gem) better_errors-0.7.0/lib/better_errors/middleware.rb:84:in `protected_app_call' (gem) better_errors-0.7.0/lib/better_errors/middleware.rb:79:in `better_errors_call' (gem) better_errors-0.7.0/lib/better_errors/middleware.rb:56:in `call' (gem) actionpack-3.2.11/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call' (gem) actionpack-3.2.11/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' (gem) railties-3.2.11/lib/rails/rack/logger.rb:32:in `call_app' (gem) railties-3.2.11/lib/rails/rack/logger.rb:16:in `block in call' (gem) activesupport-3.2.11/lib/active_support/tagged_logging.rb:22:in `tagged' (gem) railties-3.2.11/lib/rails/rack/logger.rb:16:in `call' (gem) actionpack-3.2.11/lib/action_dispatch/middleware/request_id.rb:22:in `call' (gem) rack-1.4.5/lib/rack/methodoverride.rb:21:in `call' (gem) rack-1.4.5/lib/rack/runtime.rb:17:in `call' (gem) activesupport-3.2.11/lib/active_support/cache/strategy/local_cache.rb:72:in `call' (gem) rack-1.4.5/lib/rack/lock.rb:15:in `call' (gem) actionpack-3.2.11/lib/action_dispatch/middleware/static.rb:62:in `call' (gem) railties-3.2.11/lib/rails/engine.rb:479:in `call' (gem) railties-3.2.11/lib/rails/application.rb:223:in `call' (gem) rack-1.4.5/lib/rack/content_length.rb:14:in `call' (gem) railties-3.2.11/lib/rails/rack/log_tailer.rb:17:in `call' (gem) rack-1.4.5/lib/rack/handler/webrick.rb:59:in `service' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload