{"action":"opened","issue":{"url":"https://api.github.com/repos/rspec/rspec-rails/issues/1400","labels_url":"https://api.github.com/repos/rspec/rspec-rails/issues/1400/labels{/name}","comments_url":"https://api.github.com/repos/rspec/rspec-rails/issues/1400/comments","events_url":"https://api.github.com/repos/rspec/rspec-rails/issues/1400/events","html_url":"https://github.com/rspec/rspec-rails/issues/1400","id":88930435,"number":1400,"title":"PG::UndefinedFunction: ERROR:  function median(integer) does not exist","user":{"login":"EhsanZ","id":7473298,"avatar_url":"https://avatars.githubusercontent.com/u/7473298?v=3","gravatar_id":"","url":"https://api.github.com/users/EhsanZ","html_url":"https://github.com/EhsanZ","followers_url":"https://api.github.com/users/EhsanZ/followers","following_url":"https://api.github.com/users/EhsanZ/following{/other_user}","gists_url":"https://api.github.com/users/EhsanZ/gists{/gist_id}","starred_url":"https://api.github.com/users/EhsanZ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/EhsanZ/subscriptions","organizations_url":"https://api.github.com/users/EhsanZ/orgs","repos_url":"https://api.github.com/users/EhsanZ/repos","events_url":"https://api.github.com/users/EhsanZ/events{/privacy}","received_events_url":"https://api.github.com/users/EhsanZ/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-06-17T06:48:47Z","updated_at":"2015-06-17T06:48:47Z","closed_at":null,"body":"I have the following function working fine everywhere but within RSpec tests:\r\n\r\n```\r\nclass Item < ActiveRecord::Base\r\n  has_many :ratings\r\n\r\n  def median_rating\r\n    sql = \"SELECT median(value) AS median_value FROM ratings WHERE item_id='#{self.id}'\"\r\n    median = ActiveRecord::Base.connection.execute(sql)\r\n    median[0]['median_value']\r\n  end\r\n\r\nend\r\n```\r\n\r\nWhen I run controller tests that render a view, and the `median_rating` function is being called within that view, I get the following error:\r\n\r\n```\r\nActionView::Template::Error:\r\n       PG::UndefinedFunction: ERROR:  function median(integer) does not exist\r\n       LINE 1: SELECT median(value) AS median_value FROM ratings WHERE item...\r\n                      ^\r\n       HINT:  No function matches the given name and argument types. You might need to add explicit type casts.\r\n       : SELECT median(value) AS median_value FROM ratings WHERE item_id='5'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'\r\n     # ./app/models/item.rb:20:in `median_rating'\r\n     # ./app/views/contests/show.html.erb:26:in `block in _app_views_contests_show_html_erb___2285056556312592126_70152695323800'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/relation/delegation.rb:132:in `each'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/relation/delegation.rb:132:in `each_with_index'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/relation/delegation.rb:132:in `public_send'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/relation/delegation.rb:132:in `method_missing'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/relation/delegation.rb:99:in `method_missing'\r\n     # ./app/views/contests/show.html.erb:14:in `_app_views_contests_show_html_erb___2285056556312592126_70152695323800'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/template.rb:145:in `block in render'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `block in instrument'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `instrument'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/template.rb:333:in `instrument'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/template.rb:143:in `render'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `block in instrument'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `instrument'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/renderer/template_renderer.rb:52:in `render_template'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/renderer/template_renderer.rb:14:in `render'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/renderer/renderer.rb:42:in `render_template'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/renderer/renderer.rb:23:in `render'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/rendering.rb:100:in `_render_template'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/streaming.rb:217:in `_render_template'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/rendering.rb:83:in `render_to_body'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/rendering.rb:32:in `render_to_body'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/renderers.rb:37:in `render_to_body'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/abstract_controller/rendering.rb:25:in `render'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/rendering.rb:16:in `render'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/core_ext/benchmark.rb:12:in `ms'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:44:in `block in render'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:43:in `render'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/implicit_render.rb:10:in `default_render'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/implicit_render.rb:5:in `send_action'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/abstract_controller/base.rb:198:in `process_action'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/rendering.rb:10:in `process_action'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/abstract_controller/callbacks.rb:20:in `block in process_action'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:117:in `call'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:117:in `call'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:505:in `call'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:505:in `call'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:92:in `_run_callbacks'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/abstract_controller/callbacks.rb:19:in `process_action'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/rescue.rb:29:in `process_action'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `block in instrument'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/notifications.rb:164:in `instrument'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'\r\n     # /Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/railties/controller_runtime.rb:18:in `process_action'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/abstract_controller/base.rb:137:in `process'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionview-4.2.1/lib/action_view/rendering.rb:30:in `process'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:632:in `process'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:65:in `process'\r\n     # /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:508:in `get'\r\n     # ./spec/controllers/contests_controller_spec.rb:85:in `block (5 levels) in <top (required)>'\r\n     # ./spec/rails_helper.rb:49:in `block (3 levels) in <top (required)>'\r\n     # /Library/Ruby/Gems/2.0.0/gems/database_cleaner-1.4.1/lib/database_cleaner/generic/base.rb:15:in `cleaning'\r\n     # /Library/Ruby/Gems/2.0.0/gems/database_cleaner-1.4.1/lib/database_cleaner/base.rb:92:in `cleaning'\r\n     # /Library/Ruby/Gems/2.0.0/gems/database_cleaner-1.4.1/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'\r\n     # /Library/Ruby/Gems/2.0.0/gems/database_cleaner-1.4.1/lib/database_cleaner/configuration.rb:87:in `call'\r\n     # /Library/Ruby/Gems/2.0.0/gems/database_cleaner-1.4.1/lib/database_cleaner/configuration.rb:87:in `cleaning'\r\n     # ./spec/rails_helper.rb:48:in `block (2 levels) in <top (required)>'\r\n```"}}