所以,我对编程很陌生,但是我爱上了 Rails 而不是其他应用程序,但是我尝试过 Railscasts、rubyonrails.org 和其他几个地方,但我真的迷路了。
在控制器“weekly_review_controller.rb”中,我有:
def show
@weeklyReviewData = []
currentBusinessEmployees = current_user.thisUsersBusiness.employees.uniq
@weekStart = "Sun, 2 Dec 2012 00:00 -0500".to_datetime
@weekEnd = @weekStart+7.days-1.second
currentBusinessEmployees.each do |t|
# Vars
@employeeWeeklyData = []
# Names
@employeeWeeklyData << t.firstName + " " + t.lastName
# DailyData
dStop = t.dailyData.select("deliveryStop").where( "startTime > ? AND endTime < ?", @weekStart, @weekEnd )
dPkg = t.dailyData.select("deliveryPackage").where( "startTime > ? AND endTime < ?", @weekStart, @weekEnd )
pStop = t.dailyData.select("pickupStop").where( "startTime > ? AND endTime < ?", @weekStart, @weekEnd )
pPkg = t.dailyData.select("pickupPackage").where( "startTime > ? AND endTime < ?", @weekStart, @weekEnd )
iLS = t.dailyData.select("inboundLocalService").where( "startTime > ? AND endTime < ?", @weekStart, @weekEnd )
@employeeWeeklyData << dStop << dPkg << pStop << pPkg << iLS
@weeklyReviewData << @employeeWeeklyData
end
respond_to do |format|
format.html # index.html.haml
format.json { render json: @weeklyReviewData }
end
end
所以我还没有想出传递 url param
(因此开始和结束日期的静态定义,即使我确实有传递 UTC 时间码“ /weekly_review/2012-12-02%2013:00:00%20UTC
”的 URL),但是当我多学一点。我关注的是,因为我有一个非常漂亮的视图,(问我会发布),我想用我收集的数据填充我在视图中制作的表格。问题是传递给视图的变量很好地@weeklyReviewData
填充了员工姓名(t.firstName + " " + t.lastName
),但其余数据都是Active:Relation:
混乱的。我已经看过/读过,并且很确定这样做的目的是在缓存中存储一些来自数据库的数据lazy lookups
(或者人们似乎将其称为),但这也是另一天。我只想要数据库中的数据就像名称一样。
那么我的以下查询有什么问题?:
dStop = t.dailyData.select("deliveryStop").where( "startTime > ? AND endTime < ?", @weekStart, @weekEnd )
dPkg = t.dailyData.select("deliveryPackage").where( "startTime > ? AND endTime < ?", @weekStart, @weekEnd )
pStop = t.dailyData.select("pickupStop").where( "startTime > ? AND endTime < ?", @weekStart, @weekEnd )
pPkg = t.dailyData.select("pickupPackage").where( "startTime > ? AND endTime < ?", @weekStart, @weekEnd )
iLS = t.dailyData.select("inboundLocalService").where( "startTime > ? AND endTime < ?", @weekStart, @weekEnd )
试验/笔记:
我什至使用了 rails 控制台中的查询并完成了puts t.dailyData.select("deliveryStop").where( "startTime > ? AND endTime < ?", "Sun, 2 Dec 2012 00:00 -0500".to_datetime, "Sat, 9 Dec 2012 11:59 -0500".to_datetime ).to_sql
,SQL 查询似乎很合法,但显然我遗漏了一些东西。
SQL put 是:SELECT deliveryStop FROM "daily_data" INNER JOIN "daily_data_employees" ON "daily_data"."id" = "daily_data_employees"."dailyDatum_id" WHERE "daily_data_employees"."employee_id" = 2 AND (startTime > '2012-12-02 05:00:00' AND endTime < '2012-12-09 16:59:00')