Reputation: 6679
I have a dynamic view in my database. I am using Postgres and this is my view
CREATE VIEW CONSULTANTS_CLIENTS_TASKS AS
SELECT
concat_ws(' ', consultants.first_name, consultants.last_name) as consultant_name,
clients.name as client_name,
tasks.name as task_name,
tasks.init_time::date as task_date,
EXTRACT (epoch FROM tasks.finish_time - tasks.init_time)/3600 as task_duration,
consultants_select.consultant_cost_per_hour * (EXTRACT (epoch FROM tasks.finish_time - tasks.init_time))/3600 as consultant_cost,
clients_select.client_amount_per_hour * (EXTRACT (epoch FROM tasks.finish_time - tasks.init_time))/3600 as client_amount,
task_related_costs_per_hour * (EXTRACT (epoch FROM tasks.finish_time - tasks.init_time))/3600 as task_cost_related
FROM consultants
INNER JOIN tasks ON consultants.id = tasks.consultant_id
INNER JOIN clients ON tasks.client_id = clients.id
INNER JOIN
(SELECT
consultants.id as consultant_id,
salaries.payment / (extract(epoch from sum(tasks.finish_time - tasks.init_time)) / 3600) as consultant_cost_per_hour
FROM consultants
INNER JOIN tasks ON consultants.id = tasks.consultant_id
INNER JOIN salaries ON consultants.id = salaries.consultant_id
GROUP BY consultants.id, to_char(salaries.payment_date, 'yyyy-mm'), salaries.payment
ORDER BY consultant_id) AS consultants_select ON consultants.id = consultants_select.consultant_id
INNER JOIN
(SELECT
clients.id as client_id,
amount_client_month.amount_client / worked_client_month.worked_hours as client_amount_per_hour
FROM
(SELECT
clients.id as client_id,
to_char(purchases.date, 'yyyy-mm') as client_year_month,
SUM(products.price) as amount_client
FROM clients
INNER JOIN purchases ON purchases.client_id = clients.id
INNER JOIN products ON purchases.product_id = products.id
GROUP BY clients.id, to_char(purchases.date, 'yyyy-mm')
ORDER BY clients.id) AS amount_client_month
INNER JOIN
(SELECT
clients.id as client_id,
to_char(tasks.init_time::date, 'yyyy-mm') as month,
SUM(extract(epoch from (tasks.finish_time - tasks.init_time))) / 3600 as worked_hours
FROM tasks
INNER JOIN clients ON clients.id = tasks.client_id
GROUP BY clients.id, to_char(tasks.init_time::date, 'yyyy-mm')) as worked_client_month
ON amount_client_month.client_id = worked_client_month.client_id
INNER JOIN clients ON worked_client_month.client_id = clients.id) AS clients_select ON clients.id = clients_select.client_id
INNER JOIN
(SELECT
tasks.id as task_id,
SUM(costs.amount) as task_related_costs_per_hour
FROM costs
INNER JOIN task_costs ON costs.id = task_costs.cost_id
INNER JOIN tasks ON task_costs.task_id = tasks.id
GROUP BY tasks.id) AS tasks_select ON tasks.id = tasks_select.task_id;
Inside views I have the folder reports\consultants_clients_tasks
<h1>Report: ConsultantsClientsTasks</h1>
<table class="table">
<thead>
<tr>
<th>consultant_name</th>
<th>client_name</th>
<th>task_name</th>
<th>task_date</th>
<th>task_duration</th>
<th>consultant_cost</th>
<th>client_amount</th>
<th>task_cost_related</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @consultants_clients_tasks.each do |co| %>
<tr>
<td><%= co.consultant_name %></td>
<td><%= co.client_name %></td>
<td><%= co.task_name %></td>
<td><%= co.task_date %></td>
<td><%= co.task_duration %></td>
<td><%= co.consultant_cost %></td>
<td><%= co.client_amount %></td>
<td><%= co.task_cost_related %></td>
</tr>
<% end %>
</tbody>
</table>
Inside controllers I have a folder call reports
with the file consultants_clients_tasks_controller.rb
class Reports::ConsultantsClientsTasksController < ApplicationController
def index
@consultants_clients_tasks = ConsultantsClientsTask.all
end
end
Inside models I have the folder reports
with the file consultants_clients_task.rb
module ReadOnlyModel
extend ActiveSupport::Concern
class Reports::ConsultantsClientsTask < ActiveRecord::Base
self.table_name = "consultants_clients_tasks"
end
end
and in the routes files I added
namespace :reports do
get 'consultants_clients_tasks/index'
end
The first time I load into the page all works fine, the problem is when I refresh the page I get this errors
Upvotes: 1
Views: 36
Reputation: 33542
The error is obvious as you are using a wrong class name. You need to change
@consultants_clients_tasks = ConsultantsClientsTask.all
to
@consultants_clients_tasks = Reports::ConsultantsClientsTask.all
Upvotes: 2