多态关联 - 找不到id = 2254的栏

时间:2014-04-05 19:01:18

标签: ruby-on-rails ruby-on-rails-4

我使用Factual API和其他API。我想对所有内容使用注释(多态关联) - 对于使用Factual API的栏和其他API的其他内容。

class CommentsController < ApplicationController
  before_filter :load_commentable

  ...

  def load_commentable
    resource, id = request.path.split('/')[1, 2]
    @commentable = resource.singularize.classify.constantize.find(id)      <-----
  end

创建评论的表格

<%= link_to "New Comment", [:new, @commentable, :comment] %>

bars_controller

def show

    query = @factual.table('places')
    @bar = query.filters('factual_id' => params[:id]).first

    @commentable = @bar
    @comments = @commentable.comments
    @comment = Comment.new

end

酒吧模特

class Bar < ActiveRecord::Base;

  has_many :comments, as: :commentable

end

我使用了this教程。此外,当我有普通的文章模型(在应用程序中创建)时,它的工作完全正常。当使用API​​时,当我得到错误时就是这样。

服务器日志

Started GET "/bars/472028cb-ded6-489b-94b1-21fd3ec9d35c/comments/new" for 92.241.140.218 at 2014-04-05 20:13:00 +0000                                                                
Processing by CommentsController#new as HTML                                                                                                                                           
  Parameters: {"bar_id"=>"472028cb-ded6-489b-94b1-21fd3ec9d35c"}                                                                                                                    
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]                                                                                        
  Bar Load (0.2ms)  SELECT "bars".* FROM "bars" WHERE "bars"."id" = ? LIMIT 1  [["id", "472028cb-ded6-489b-94b1-21fd3ec9d35c"]]                                               
Completed 404 Not Found in 52ms                                                                                                                                                       

ActiveRecord::RecordNotFound - Couldn't find Bar with id=472028cb-ded6-489b-94b1-21fd3ec9d35c:                                                                                      
  activerecord (4.0.0) lib/active_record/relation/finder_methods.rb:198:in `raise_record_not_found_exception!'                                                                        
  activerecord (4.0.0) lib/active_record/relation/finder_methods.rb:284:in `find_one'                                                                                                 
  activerecord (4.0.0) lib/active_record/relation/finder_methods.rb:268:in `find_with_ids'                                                                                            
  activerecord (4.0.0) lib/active_record/relation/finder_methods.rb:35:in `find'                                                                                                      
  activerecord-deprecated_finders (1.0.3) lib/active_record/deprecated_finders/relation.rb:122:in `find'                                                                              
  activerecord (4.0.0) lib/active_record/querying.rb:3:in `find'                                                                                                                      
  app/controllers/comments_controller.rb:67:in `load_commentable'                                                                                                                       
  activesupport (4.0.0) lib/active_support/callbacks.rb:447:in `_run__1736236977553759703__process_action__callbacks'                                                                 
  activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'                                                                                                         
  actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'                                                                                                      
  actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'                                                                                                     
  actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'                                                                                   
  activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'                                                                                              
  activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'                                                                                           
  activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'                                                                                                       
  actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'                                                                                            
  actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'                                                                                            
  activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'                                                                                        
  actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'                                                                                                                 
  actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'                                                                                                             
  actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'                                                                                                                 
  actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'                                                                                                  
  actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'                                                                                                          
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'                                                                                                        
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'                                                                                                            
  actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'                                                                                                      
  actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'                                                                                                               
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'                                                                                                           
  omniauth (1.2.1) lib/omniauth/strategy.rb:186:in `call!'  
omniauth (1.2.1) lib/omniauth/strategy.rb:164:in `call'                                                                                                                             
  omniauth (1.2.1) lib/omniauth/builder.rb:59:in `call'                                                                                                                               
  meta_request (0.2.8) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'                                                                                               
  rack-contrib (1.1.0) lib/rack/contrib/response_headers.rb:17:in `call'                                                                                                              
  meta_request (0.2.8) lib/meta_request/middlewares/headers.rb:16:in `call'                                                                                                           
  meta_request (0.2.8) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'                                                                                              
  newrelic_rpm (3.7.3.204) lib/new_relic/rack/error_collector.rb:55:in `call'                                                                                                         
  newrelic_rpm (3.7.3.204) lib/new_relic/rack/agent_hooks.rb:32:in `call'                                                                                                             
  newrelic_rpm (3.7.3.204) lib/new_relic/rack/browser_monitoring.rb:27:in `call'                                                                                                      
  rack (1.5.2) lib/rack/etag.rb:23:in `call'                                                                                                                                          
  rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'                                                                                                                                
  rack (1.5.2) lib/rack/head.rb:11:in `call'                                                                                                                                          
  actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'                                                                                                     
  actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'                                                                                                            
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'                                                                                                                       
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'                                                                                                                          
  actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'                                                                                                          
  activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'                                                                                                                  
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'                                                                                
  activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'                                                                                                                   
  actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'                                                                                                
  activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__3544743962306853787__call__callbacks'                                                                           
  activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'                                                                                                         
  actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'                                                                                                         
  actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'                                                                                                          
  actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'                                                                                                         
  better_errors (1.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'                                                                                                    
  better_errors (1.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'                                                                                                    
  better_errors (1.1.0) lib/better_errors/middleware.rb:56:in `call'                                                                                                                  
  actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'                                                                                                  
  actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'                                                                                                   
  railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'                                                                                                                          
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'                                                                                                                     
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'                                                                                                  
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'   
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'                                                                                                           
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'                                                                                                                              
  actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'                                                                                                        
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'                                                                                                                                
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'                                                                                                                                       
  activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'                                                                                                 
  rack (1.5.2) lib/rack/lock.rb:17:in `call'                                                                                                                                          
  actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'                                                                                                            
  railties (4.0.0) lib/rails/engine.rb:511:in `call'                                                                                                                                  
  railties (4.0.0) lib/rails/application.rb:97:in `call'                                                                                                                              
  rack (1.5.2) lib/rack/lock.rb:17:in `call'                                                                                                                                          
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'                                                                                                                                
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'                                                                                                                            
  /home/action/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'                                                                                      
  /home/action/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'                                                                                           
  /home/action/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

模式

  create_table "comments", force: true do |t|
    t.string   "title"
    t.text     "content"
    t.integer  "user_id"
    t.integer  "commentable_id"
    t.string   "commentable_type"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

 create_table "bars", force: true do |t|
t.integer  "factual_id"
t.datetime "created_at"
t.datetime "updated_at"
  end

1 个答案:

答案 0 :(得分:1)

load_commentable 替换为:

def load_commentable
    resource, id = request.path.split('/')[1, 2]
    resource_name = resource.singularize.classify
    if resource_name == "Bar"
      @factual = Factual.new("...", "...") ## Add your keys
      @factual.table('places').query.filters('factual_id' => id).first
    else
       @commentable = resource_name.constantize.find(id)      
    end 
end

此外,根据聊天会话,factual_id需要存储在Bar模型中:

   ## BarsController
    def show
        query = @factual.table('places')
        @bar = query.filters('factual_id' => params[:id]).first
        @commentable = Bar.find_by(factual_id: params[:id])
        @comments = @commentable.comments
        @comment = Comment.new
    end