Reputation: 1246
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.
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.
def forresource
resource_id = params[:id]
@comments = Resource.find_by_id(resource_id).comments
respond_to do |format|
format.html
format.json { render :status=>200, :json=>{:success=>true, :comments => @comments}}
end
end
However, when run, this method seems to be searching for comments with a comment_id instead.
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"=>"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:
Can anybody tell me what I'm doing wrong? I've also tried the command.
@comments = Comment.where(:resource_id => resource_id)
Here are the abbreviated model relationships
class Resource < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
attr_accessible :content, :resource_id, :user_id
belongs_to :user
belongs_to :resource
end
Thanks for the help! I've spent the last 3 days beating my head against a wall trying to fix this.
Update: Here's the stacktrace from my error:
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"=>"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'
Upvotes: 2
Views: 141
Reputation: 4943
You mentioned using CanCan, are you perhaps using load_and_authorize_resource or load_resource? If so, you'll need to exclude those actions from this controller so that CanCan doesn't try to load a comment from your resourceful routes. Alternately, you can change your routing so that the resources have comments "inside" of them routing-wise.
The latter approach is the cleanest, resourceful routes would generally have /resources/:resource_id/comments in your scenario.
Upvotes: 3
Reputation: 9693
I've tried:
class Resource < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
attr_accessible :content, :resource_id, :user_id
belongs_to :user
belongs_to :resource
end
and works as expected
1.9.3-p392 :026 > Comment.where(:resource_id => resource_id).to_sql
=> "SELECT `comments`.* FROM `comments` WHERE `comments`.`resource_id` = 5"
1.9.3-p392 :028 > Resource.where(id: 1).first.comments.to_sql
Resource Load (1.9ms) SELECT `resources`.* FROM `resources` WHERE `resources`.`id` = 1 LIMIT 1
=> "SELECT `comments`.* FROM `comments` WHERE `comments`.`resource_id` = 1"
Upvotes: 0