2

ActiveRecords 有一个奇怪的问题,我已经为此苦苦挣扎了几天。我仍然是 Rails 的初学者,希望 stackoverflow 上的一位 Rails 专家能告诉我我做错了什么。

我正在使用 postgres 服务器,并且资源和评论之间存在多对一的关系。我在控制器上有一个操作,我通过 ajax 调用它来检索特定资源的所有评论。我使用这种方法应该返回属于特定资源 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

但是,在运行时,此方法似乎是在搜索带有 comment_id 的评论。

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:

谁能告诉我我做错了什么?我也试过这个命令。

@comments = Comment.where(:resource_id => resource_id)

这是缩写的模型关系

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

谢谢您的帮助!在过去的 3 天里,我一直在用头撞墙试图解决这个问题。

更新:这是我的错误的堆栈跟踪:

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'
4

2 回答 2

3

您提到使用 CanCan,您可能使用的是 load_and_authorize_resource 还是 load_resource?如果是这样,您需要从该控制器中排除这些操作,以便 CanCan 不会尝试从您的资源丰富的路由中加载评论。或者,您可以更改您的路由,以便资源在路由方面具有“内部”注释。

后一种方法是最干净、资源丰富的路线,在您的场景中通常会有 /resources/:resource_id/comments。

于 2013-03-05T03:34:30.207 回答
0

我试过了:

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

并按预期工作

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" 
于 2013-03-05T03:13:34.660 回答