리소스의:인덱스 블록에서 SQL 조건 또는 조인을 결정할 수 없음

9425 단어 cancan

묘사

제품 모델과 컨트롤러가 하나 있습니다.내 색인 방법은 다음과 같습니다.

def index
    @req_host_w_port = request.host_with_port
    logger.info "current_ability = #{current_ability.inspect}"
    @products = Product.accessible_by(current_ability, :index)
  end
나는 그곳에서 제품을 검색하려고 시도했을 때 오류가 발생했다.나의 제품 능력 과정은 다음과 같다.

can :index, Product do |product|
        product && !(product.approved_at.nil?)
      end
오류는 다음과 같습니다.

Cannot determine SQL conditions or joins from block for :index Product(id: integer, secret: string, context_date: datetime, expiration_date: datetime, blurb: text, created_at: datetime, updated_at: datetime, owner_id: integer, product_type_id: integer, approved_at: datetime)
나는 왜 이런 상황이 발생했는지 잘 모르겠다. 왜냐하면 이것은 매우 간단한 예인 것 같지만, 나는 몇몇 이상한 상황이 can 블록에서 발생하는 것을 보았기 때문에 아마도 이것이 문제일 것이다.

토론 #1

이 문제를 상세하게 설명하기 위해 아래의 두 단락의 코드는 등가가 아니다. 비록 그들은 등가로 보이지만.맨 위 부분은 정상적으로 작동하고 맨 아래 부분은 오류가 발생했습니다.can :index, Product, :approved_at => nil

can :index, Product do |product|
    product && product.approved_at.nil?
end

cancan (1.3.2) lib/cancan/ability.rb:219:in `relevant_can_definitions_for_query'
cancan (1.3.2) lib/cancan/ability.rb:217:in `each'
cancan (1.3.2) lib/cancan/ability.rb:217:in `relevant_can_definitions_for_query'
cancan (1.3.2) lib/cancan/ability.rb:189:in `query'
cancan (1.3.2) lib/cancan/active_record_additions.rb:23:in `accessible_by'
app/controllers/products_controller.rb:12:in `index'
actionpack (3.0.0.beta4) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.0.beta4) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.0.beta4) lib/abstract_controller/base.rb:145:in `process_action'
actionpack (3.0.0.beta4) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.0.beta4) lib/abstract_controller/callbacks.rb:18:in `process_action'
activesupport (3.0.0.beta4) lib/active_support/callbacks.rb:438:in `_run__1118320962__process_action__199225275__callbacks'
activesupport (3.0.0.beta4) lib/active_support/callbacks.rb:408:in `send'
activesupport (3.0.0.beta4) lib/active_support/callbacks.rb:408:in `_run_process_action_callbacks'
activesupport (3.0.0.beta4) lib/active_support/callbacks.rb:88:in `send'
activesupport (3.0.0.beta4) lib/active_support/callbacks.rb:88:in `run_callbacks'
actionpack (3.0.0.beta4) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.0.beta4) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
activesupport (3.0.0.beta4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.0.0.beta4) lib/active_support/notifications.rb:48:in `__send__'
activesupport (3.0.0.beta4) lib/active_support/notifications.rb:48:in `instrument'
actionpack (3.0.0.beta4) lib/action_controller/metal/instrumentation.rb:28:in `process_action'
actionpack (3.0.0.beta4) lib/action_controller/metal/rescue.rb:8:in `process_action'
actionpack (3.0.0.beta4) lib/abstract_controller/base.rb:114:in `process'
actionpack (3.0.0.beta4) lib/abstract_controller/rendering.rb:40:in `process'
actionpack (3.0.0.beta4) lib/action_controller/metal.rb:126:in `dispatch'
actionpack (3.0.0.beta4) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.0.beta4) lib/action_controller/metal.rb:166:in `action'
actionpack (3.0.0.beta4) lib/action_dispatch/routing/route_set.rb:27:in `call'
actionpack (3.0.0.beta4) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.10) lib/rack/mount/route_set.rb:148:in `call'
rack-mount (0.6.10) lib/rack/mount/code_generation.rb:89:in `recognize'
rack-mount (0.6.10) lib/rack/mount/code_generation.rb:66:in `optimized_each'
rack-mount (0.6.10) lib/rack/mount/code_generation.rb:88:in `recognize'
rack-mount (0.6.10) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.0.beta4) lib/action_dispatch/routing/route_set.rb:457:in `call'
warden (0.10.7) lib/warden/manager.rb:35:in `call'
warden (0.10.7) lib/warden/manager.rb:34:in `catch'
warden (0.10.7) lib/warden/manager.rb:34:in `call'
actionpack (3.0.0.beta4) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.1.0) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.0.beta4) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.0.beta4) lib/action_dispatch/middleware/flash.rb:177:in `call'
actionpack (3.0.0.beta4) lib/action_dispatch/middleware/session/abstract_store.rb:106:in `call'
actionpack (3.0.0.beta4) lib/action_dispatch/middleware/cookies.rb:235:in `call'
activerecord (3.0.0.beta4) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.0.beta4) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.0.beta4) lib/active_record/query_cache.rb:11:in `cache'
activerecord (3.0.0.beta4) lib/active_record/query_cache.rb:30:in `call'
activerecord (3.0.0.beta4) lib/active_record/connection_adapters/abstract/connection_pool.rb:365:in `call'
actionpack (3.0.0.beta4) lib/action_dispatch/middleware/callbacks.rb:46:in `call'
activesupport (3.0.0.beta4) lib/active_support/callbacks.rb:414:in `_run_call_callbacks'
activesupport (3.0.0.beta4) lib/active_support/callbacks.rb:88:in `send'
activesupport (3.0.0.beta4) lib/active_support/callbacks.rb:88:in `run_callbacks'
actionpack (3.0.0.beta4) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.1.0) lib/rack/sendfile.rb:105:in `call'
actionpack (3.0.0.beta4) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.0.beta4) lib/action_dispatch/middleware/show_exceptions.rb:48:in `call'
railties (3.0.0.beta4) lib/rails/rack/logger.rb:14:in `call'
rack (1.1.0) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.0.beta4) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.1.0) lib/rack/lock.rb:11:in `call'
rack (1.1.0) lib/rack/lock.rb:11:in `synchronize'
rack (1.1.0) lib/rack/lock.rb:11:in `call'
actionpack (3.0.0.beta4) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.0.beta4) lib/rails/application.rb:145:in `call'
railties (3.0.0.beta4) lib/rails/application.rb:81:in `send'
railties (3.0.0.beta4) lib/rails/application.rb:81:in `method_missing'
railties (3.0.0.beta4) lib/rails/rack/log_tailer.rb:15:in `call'
rack (1.1.0) lib/rack/content_length.rb:13:in `call'
rack (1.1.0) lib/rack/handler/webrick.rb:48:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
rack (1.1.0) lib/rack/handler/webrick.rb:14:in `run'
rack (1.1.0) lib/rack/server.rb:155:in `start'
railties (3.0.0.beta4) lib/rails/commands/server.rb:62:in `start'
railties (3.0.0.beta4) lib/rails/commands.rb:30
railties (3.0.0.beta4) lib/rails/commands.rb:27:in `tap'
railties (3.0.0.beta4) lib/rails/commands.rb:27
script/rails:6:in `require'
script/rails:6

토론 #2

더 많은 추적을 통해 나는 문제점을 발견했지만 이것이 진정한 잘못인지 내가 무엇을 잘못했는지 확실하지 않다.근데 벌레 같애.CanCan의 역량 클래스 구현에서 이 함수는 질의의 can 정의를 가져옵니다.

def relevant_can_definitions_for_query(action, subject)
      relevant_can_definitions(action, subject).each do |can_definition|
        if can_definition.only_block?
          raise Error, "Cannot determine SQL conditions or joins from block for #{action.inspect} #{subject.inspect}"
        end
      end
    end
만약 can이 블록이 있지만 조건이 없다면 only_block? 함수는true를 되돌려줍니다.캔의 정의가 어떻게 초기화되었는지 알게 된 후에 이것은 나에게 큰 의미가 없다.자술한 파일의 예시 (아래 참조) 에는 블록이 하나 있지만, 조건은 없지만, 정상적으로 작동해야 한다.
이것은 버그가 아니라 라이브러리의 제한입니다.이 질문에 대한 답변을 참조하십시오.
http://stackoverflow.com/questions/3557091/using-blocks-to-define-abilities-in-cancan-raises-an-exception/3557445#3557445 나는 자술한 파일을 더욱 분명하게 업데이트할 것이다.헷갈려서 미안해. 4fe44af45d94cfc275973d5e7b89fbb2443fe9cd가 닫혔을 때 사용할 수 없는 블록을 더 잘 알 수 있습니다

좋은 웹페이지 즐겨찾기