Ruby - 使用activerecord排除列

时间:2017-02-28 10:58:02

标签: ruby join activerecord

我想在Check表中排除一个列(称为checkdata)。

现在我的代码看起来像这样:

ret = Check.joins(:model).get_list do |q|
  // some code
end

我尝试了以下内容,因为我正在寻找解决方案,但它给了我一个错误:

ret = Check.select(Check.attribute_names - ['checkdata']).joins(:model).get_list do |q|
  // some code
end

错误:

E, [2017-02-28T10:45:05.154053 #10874] ERROR -- : SQLite3::SQLException: ambiguous column name: id: SELECT COUNT(id, unit_id, model_id, checktype_id, comment, scheduled, dueby, started, finished, status, metadata, created_at, updated_at, created_by, updated_by) FROM "checks" INNER JOIN "models" ON "models"."id" = "checks"."model_id" WHERE "checks"."status" = 20 2017-02-28 10:45:05 - ActiveRecord::StatementInvalid - SQLite3::SQLException: ambiguous column name: id: SELECT COUNT(id, unit_id, model_id, checktype_id, comment, scheduled, dueby, started, finished, status, metadata, created_at, updated_at, created_by, updated_by) FROM "checks" INNER JOIN "models" ON "models"."id" = "checks"."model_id" WHERE "checks"."status" = 20:
        /Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `initialize'
        /Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `new'
        /Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `prepare'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/sqlite3_adapter.rb:301:in `block in exec_query'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract_adapter.rb:378:in `block in log'
        /Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.16/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract_adapter.rb:372:in `log'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/sqlite3_adapter.rb:298:in `exec_query'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/sqlite3_adapter.rb:510:in `select'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/calculations.rb:265:in `execute_simple_calculation'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/calculations.rb:227:in `perform_calculation'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/calculations.rb:119:in `calculate'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/calculations.rb:34:in `count'
        /Users/ndinatale/leanlogic-qa-prototype/server/lib/models_base.rb:135:in `get_list'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/delegation.rb:94:in `public_send'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/delegation.rb:94:in `block in method_missing'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation.rb:292:in `scoping'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/delegation.rb:94:in `method_missing'
        /Users/ndinatale/leanlogic-qa-prototype/server/lib/routes_dashboard.rb:110:in `dashboard_type_CPCH'
        /Users/ndinatale/leanlogic-qa-prototype/server/routes/02_rest_misc.rb:175:in `call'
        /Users/ndinatale/leanlogic-qa-prototype/server/routes/02_rest_misc.rb:175:in `block in <top (required)>'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1611:in `call'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1611:in `block in compile!'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:975:in `[]'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:975:in `block (3 levels) in route!'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:994:in `route_eval'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:975:in `block (2 levels) in route!'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1015:in `block in process_route'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1013:in `catch'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1013:in `process_route'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:973:in `block in route!'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:972:in `each'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:972:in `route!'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1085:in `block in dispatch!'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `block in invoke'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `catch'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `invoke'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1082:in `dispatch!'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:907:in `block in call!'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `block in invoke'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `catch'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `invoke'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:907:in `call!'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:895:in `call'
        /Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/session/abstract/id.rb:225:in `context'
        /Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/session/abstract/id.rb:220:in `call'
        /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
        /Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/xss_header.rb:18:in `call'
        /Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/path_traversal.rb:16:in `call'
        /Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/json_csrf.rb:18:in `call'
        /Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
        /Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
        /Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/frame_options.rb:31:in `call'
        /Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/nulllogger.rb:9:in `call'
        /Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/head.rb:13:in `call'
        /Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/methodoverride.rb:22:in `call'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/show_exceptions.rb:25:in `call'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:182:in `call'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:2013:in `call'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1487:in `block in call'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1785:in `synchronize'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1785:in `synchronize'
        /Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1487:in `call'
        /Library/Ruby/Gems/2.0.0/gems/puma-3.7.0/lib/puma/configuration.rb:226:in `call'
        /Library/Ruby/Gems/2.0.0/gems/puma-3.7.0/lib/puma/server.rb:578:in `handle_request'
        /Library/Ruby/Gems/2.0.0/gems/puma-3.7.0/lib/puma/server.rb:415:in `process_client'
        /Library/Ruby/Gems/2.0.0/gems/puma-3.7.0/lib/puma/server.rb:275:in `block in run'
        /Library/Ruby/Gems/2.0.0/gems/puma-3.7.0/lib/puma/thread_pool.rb:120:in `call'
        /Library/Ruby/Gems/2.0.0/gems/puma-3.7.0/lib/puma/thread_pool.rb:120:in `block in spawn_thread'

有人可以帮我吗?谢谢。

1 个答案:

答案 0 :(得分:0)

它看起来与联接表冲突,因为它们都有id列。试试这个:

cols = (Check.attribute_names - ['checkdata']).map { |attrib| "checks.#{attrib}" }
ret = Check.select(*cols).joins(:model).get_list ...