I'm trying to map an existing PostgreSQL view (which creation script was ran and tested in a different computer) to a Rails application, but I keep getting PG::UndefinedTable: ERROR: relation "reports_consultants_clients_tasks" does not exist.
First, this is my routes file:
Rails.application.routes.draw do
namespace :reports do
get 'consultants_clients_tasks/index'
end
# rest of the file
end
The controller:
class Reports::ConsultantsClientsTasksController < ApplicationController
def index
@consultants_clients_tasks = ConsultantsClientsTask.all
end
end
The model (I tried with and without table name, anyway, it should be following the convention... right?)
class Reports::ConsultantsClientsTask < ActiveRecord::Base
#self.table_name = "CONSULTANTS_CLIENTS_TASKS"
end
This is my migration to create the view:
class CreateReportsConsultantsClientsTasksSqlView < ActiveRecord::Migration
def change
execute <<-SQL
CREATE VIEW CONSULTANTS_CLIENTS_TASKS AS
-- rest of the script
;
SQL
end
end
I tried running both rake db:reset or rake db:drop db:create db:migrate and in both cases the view isn't properly created.
If I run this (the query generated by ActiveRecord), from a psql console:
SELECT "CONSULTANTS_CLIENTS_TASKS".* FROM "CONSULTANTS_CLIENTS_TASKS";
I get this error:
ERROR: relation "CONSULTANTS_CLIENTS_TASKS" does not exist
LINE 1: SELECT "CONSULTANTS_CLIENTS_TASKS".* FROM "CONSULTANTS_CLIEN...
However, running this other query:
select * from CONSULTANTS_CLIENTS_TASKS;
gets no errors:
consultant_name | client_name | task_name | task_date | task_duration | consultant_cost | client_amount | task_cost_related
-----------------+-------------+-----------+-----------+---------------+-----------------+---------------+-------------------
(0 rows)
I haven't mapped all the view's fields in my .html.erb but I don't think that's a problem.
So, any ideas on why Rails isn't able to query properly the View? Or why isn't the view properly created?
Thanks in advance
consultants_clients_tasks?consultants_clients_tasksbut your code is looking for"CONSULTANTS_CLIENTS_TASKS".