gpt4 book ai didi

ruby-on-rails - Rails 从数据库动态刷新页面 Web View

转载 作者:行者123 更新时间:2023-11-29 12:58:28 25 4
gpt4 key购买 nike

我的数据库中有一个动态 View 。我正在使用 Postgres,这是我的观点

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;

内部 View 我有文件夹 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>

在 Controller 内部,我有一个名为 reports 的文件夹,其中包含文件 consultants_clients_tasks_controller.rb

class Reports::ConsultantsClientsTasksController < ApplicationController
def index
@consultants_clients_tasks = ConsultantsClientsTask.all
end
end

在模型内部,我有文件夹 reports 和文件 consultants_clients_task.rb

module ReadOnlyModel
extend ActiveSupport::Concern
class Reports::ConsultantsClientsTask < ActiveRecord::Base
self.table_name = "consultants_clients_tasks"
end
end

在我添加的路由文件中

namespace :reports do
get 'consultants_clients_tasks/index'
end

我第一次加载到页面时一切正常,问题是当我刷新页面时出现此错误

enter image description here

最佳答案

错误很明显,因为您使用了错误的类名。你需要改变

@consultants_clients_tasks = ConsultantsClientsTask.all

@consultants_clients_tasks = Reports::ConsultantsClientsTask.all

关于ruby-on-rails - Rails 从数据库动态刷新页面 Web View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36665313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com