Time in hours and minutes... please suggest me - ruby-on-rails-3

for login_details am calculating the time between login and logout but its calculating only in minutes, want in hours and minutes. please help me and written the code as below
<table>
<%count = 1%>
<%#login_info.each do |l| %>
<tr>
<td><%= count%></td>
<td><%=l.user.name%></td>
<td><%=Location.find(l.location_id).name%></td>
<td><%=showdatetime(l.login_time)%></td>
<td><%=showdatetime(l.logout_time)%></td>
<td> <%= time=(l.logout_time.minus_with_coercion(l.login_time)/60)%>minutes</td>
</tr>
<%count = count+1%>
<%end%>
</table>

check out this code
<%count = 1%>
<%#login_info.each do |l| %>
<tr>
<td><%= count%></td>
<td><%=l.user.name%></td>
<td><%=Location.find(l.location_id).name%></td>
<td><%=showdatetime(l.login_time)%></td>
<td><%=showdatetime(l.logout_time)%></td>
<td>
<%seconds = l.logout_time.to_i - l.login_time.to_i%>
<% days = seconds / 86400%>
hours = seconds / 3600%>
<%minutes = (seconds - (hours * 3600)) / 60%>
<%= hours.to_s + "hr" + minutes.to_s + "min"%></td>
</tr>
<%count = count+1%>
<%end%>

Check out distance_of_time_in_words.

Related

Half of Classic ASP page works great, 2nd half gives SQL connection error

Please help!
I am moving a Classic ASP & ASP.Net website from one server to another, and one of the reports that works fine on the old server is only partially working on the new server.
Can someone please tell me:
If there is anything that might be preventing a successful connection to SQL server.
If there is anything else involved in the this than:
Connection String
ASP Page
DLL
My server environment:
Windows Server 2008 R2
SQL EXPRESS 2008
IIS7
Connection Details:
The Classic ASP pages of this website connect using a SQL connection string located in the registry. The other Classic ASP pages on the website are working just fine....and the first half of this report is also working just fine.
I have tested the connection string individually, and it connects properly.
Report Structure Details:
The first half of the report (that is working correctly) loads data directly from a table in the database to display each item in a list format. It also uses the EMPLOYEES table to populate the employee name for each item in the list.
The second half of the report (that is NOT working) loads data from the same table as the first half. However, it groups them by manager (Manager Details are stored in the EMPLOYEES table).
So, both half of the reports are accessing the same tables for information....the only difference is how they are displayed.
Error Details
Microsoft OLE DB Provider for SQL Server->DLL.Shape_DB.ShapeCMdWithRS(SHAPE {select * from employees where username in (select mgr_username from employees where empID in (select empID from time_off_requests where mgr_code=0)) order by last_name, first_name} APPEND ([select time_off_requests_view.*, employees.mgr_username as username from time_off_requests_view inner join employees on employees.empID = time_off_requests_view.empID where mgr_code=0} relate username to username) as timeoff, ...)#WEBSITENAME[version 4.0.480] error '80004005'
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.
Code Details
Here is the CLASSIC ASP code for the part of the website that DOES work:
set time_off_requests = DLL.TimeOffAdminPending()
%>
<CENTER>
<%
if time_off_requests.EOF then %>
<font class="headerNoBold">No Pending Time Off Requests<BR>
<% else %>
<font class="headerNoBold">Pending Time Off Requests</font>
<table cellspacing="0" cellpadding="3" width="500" align=center border=0 bordercolorlight=#000000 bordercolordark=#000000>
<TR bgcolor=#ffffcc>
<TD width=33% align=center><B>Date</B></TD>
<TD width=33% align=center><nobr><B>Requested By</B></nobr></TD>
<TD width=33% align=center><nobr><B>Type</B></nobr></TD>
</TR>
<%
dim theDate
do while not time_off_requests.EOF
if (time_off_requests.AbsolutePosition mod 2) = 0 then %>
<TR bgcolor=#ffffcc>
<% else %>
<TR bgcolor=#ffffff>
<% end if %>
<TD class="smallText" align=center>
<A HREF="time_off_request_details.asp?timeoffreq_id=<% = time_off_requests("timeoffreq_id") %>">
<% = time_off_requests("dt_created") %>
</A>
</TD>
<TD class="smallText" align=center><% = time_off_requests("last_name") %>, <% = time_off_requests("first_name") %></TD>
<TD class="smallText" align=center><% = time_off_requests("time_off_type") %></TD>
</TR>
<%
time_off_requests.MoveNext
loop %>
</TABLE>
<% end if %>
Details from the DLL
Function TimeOffAdminPending() As ADODB.Recordset
TimeOffAdminPending = RunCmdWithRS("select * from time_off_request_view where mgr_code =0 and empID in (select empID from employees where mgr_username = '" & username & "')")
End Function
Here is the CLASSIC ASP code for the part that DOES NOT work:
<% set managers = DLL.PendingTimeOffMgr()
if not managers.EOF then %>
<font class="headerNoBold">Pending Manager Time Off Approvals</font>
<table cellspacing="0" cellpadding="3" width="500" align=center border=0 bordercolorlight=#000000 bordercolordark=#000000>
<TR bgcolor=#ffffcc>
<TD width=33% align=center><B>Date</B></TD>
<TD width=33% align=center><nobr><B>Requested By</B></nobr></TD>
<TD width=33% align=center><nobr><B>Type</B></nobr></TD>
</TR>
<% do while not managers.EOF
set time_requests = managers.Fields("timeoff").Value %>
<TR><TD colspan=3>
<% = managers("last_name") %>, <% = managers("first_name") %>
</TD><TR>
<%
do while not timeoff_requests.EOF
if (timeoff_requests.AbsolutePosition mod 2) = 0 then %>
<TR bgcolor=#ffffcc>
<% else %>
<TR bgcolor=#ffffff>
<% end if %>
<TD class="smallText" align=center>
<% = timeoff_requests("dt_created") %>
</TD>
<TD class="smallText" align=center><% = timeoff_requests("last_name") %>, <% = timeoff_requests("first_name") %></TD>
<TD class="smallText" align=center><% = timeoff_requests("timeoff_type") %></TD>
</TR>
<%
timeoff_requests.MoveNext
loop
managers.MoveNext
loop %>
</TABLE>
<% end if %>
Details from the DLL
PendingTimeOffMgr() As ADODB.Recordset
PendingTimeOffMgr = ShapeCmdWithRS("SHAPE {select * from employees where username in (select mgr_username from employees where empID in (select empID from time_off_requests where mgr_code = 0)) order by last_name, first_name} APPEND ({select time_off_requests_view.*, employees.mgr_username as username from time_off_requests_view inner join employees on employees.empID=time_off_requests_view.empID where mgr_code = 0} relate username to username) as timeoff")
End function

How to get currently selected row from a resultset?

Please help me to get the currently selected row from this list. I tried to get it by sessions but it didnt work.
<% int icount=0;
while(rs.next())
{
String repairId = rs.getString("item.id");
String esn = rs.getString("esn");
String type = rs.getString("type");
session.setAttribute("repId", repairId);
session.setAttribute("repEsn", esn);
session.setAttribute("repType", type);
%>
<tr>
<td><div align="center"><%=++icount%></div></td>
<td><%=rs.getString("esn")%></td>
<td><%=rs.getString("type")%></td>
<td><%=rs.getString("technician")%></td>
<td><%=rs.getDate("date")%></td>
<td><div align="center">
<a href="LoadRepairEqipmentsServlet?repId=<%=repairId%>"
onclick="return edit()">Done Repair</a>
</div></td>
</tr>
<% } %>

Rails. how to display the data by column name

I use Rails 4.
I use structure
#users = ActiveRecord::Base.connection.execute("SELECT * FROM users")
#users.each do |row| %>
puts user[0] # id--> 1,2,3,4,5,6
end
#users.fields do |field| %>
puts field.name # --> id, login, password.....
end
how to display the data by column name?
for example
#users.first.field['id']
ActiveRecord::Base.connection.execute( will run the sql but not return anything (or return nil, more accurately). To load the records into memory do
#users = User.all
You don't describe what you want very well. If you literally want "1,2,3,4,5,6" you could do
#users.map(&:id).join(",")
if you wanted, for example, to do a table showing all the user data you could do (in your view)
<% cols = User.column_names %>
<table>
<thead>
<tr>
<% cols.each do |col| %>
<th><%= col %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<% cols.each do |col| %>
<td><%= user.send(col) %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
Like i say i don't know if this is what you actually want to do. Your requirements are a very messy and very vague.
EDIT
If you want to load data out of the database without using an AR model then you can do
#users = ActiveRecord::Base.connection.select_all("select * from users")
this will give you an array of hashes like this
[{"id" => "1", "name" => "foo", "bar" => "baz"}, {"id" => "2", "name" => "chunky", "bar" => "bacon"}, .... ]
which you could then access like
user = #users.first #you've got a hash now
aname = user["name"]

SQL that creates a new table, essentially, but with ActiveRecord

def reports
sql = "select username, email, sign_in_count, current_sign_in_at, \
(select count(*) from foo ft where ft.user_id = u.id and ft.state in ('complete', 'active', 'win', 'lose')) as foo_state, \
(select count(*) from bar where creator_id = u.id and m.state not in ('new','cancelled')) as bar_created, \
from users u where sign_in_count > 0 order by foo_state desc;"
#users = ActiveRecord::Base.connection.execute(sql)
end
So, this query is botched a bit for purposes of showing you something, but not bothering you with the details. This query takes a few different tables, queries them, and returns a new table essentially. I'm using postgreSQL, so this returns a PG::Result object. I can call #users.values to get an array of arrays, but I was wondering if there was a more 'rails-y' way do do this?
I'd like to do something like this in the view:
<%= render partial: 'users/shared/user', collection: #users, as: :user %>
but since it's not an actual ActiveRecord object, I can't seem to do that (although it doesn't throw an error if I try, it just doesn't display anything)
Can ARel accomplish a feat like this? Am I asking too much? Thanks!
For anyone else stumbling upon this... this works to get your info, at least:
<table>
<thead>
<tr>
<% #users.fields.each do |f| %>
<th><%= f %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #users.values.each do |v| %>
<tr>
<% v.each do |a| %>
<td><%= a %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>

Include controller methods in emails in a Rails 3 application?

I am trying to email a monthly report which is simple the current month's income compared to the last months.
I have all of that working in my report controller (which references another model called Clinical):
# Clinical income by month report
#clinical_income_by_month = Clinical.select(%q{date_format(transactiondate, '%Y') as year, date_format(transactiondate, '%M') as month, sum(LineBalance) as income})
.where(:payments => 0)
.where('linebalance <> ?', 0)
.where('analysiscode <> ?', 213)
.group(:monthyear)
I then have a partial for the table with the data:
<div class="row">
<div class="twelve columns"><h5>This months income so far is <span style="font-size:1.2em"class="<% if #clinical_income_by_month.first.income >= #clinical_income_by_month.first(:offset => 12).income %>green<% else %>red<% end %> radius label"><%= number_to_currency(#clinical_income_by_month.first.income, :unit => "£", :separator => ".", :delimiter => ",") %></span></h5> <%= #clinical_income_by_month.first.month %> <%= #clinical_income_by_month.first(:offset => 12).year %>'s income was <%= number_to_currency(#clinical_income_by_month.first(:offset => 12).income, :unit => "£", :separator => ".", :delimiter => ",") %>.</div>
</div>
<hr />
<table>
<tr>
<th>Year</th>
<th>Month</th>
<th>Income</th>
</tr>
<% #clinical_income_by_month.each do |c| %>
<tr>
<td><%= c.year %></td>
<td><%= c.month %></td>
<td><%= number_to_currency(c.income, :unit => "£", :separator => ".", :delimiter => ",") %></td>
</tr>
<% end %>
</table>
I would like to be able to pull that partial into my email, or if that's not possible, I would like to show the value of the last income.
<%= #clinical_income_by_month.first.income %>
My email code looks like this:
Finance Report
================================================
<%= number_to_currency(#clinical_income_by_month.first.income) %>
The error I am getting is:
1.9.2-p318 :009 > UserMailer.finance_report().deliver
ActionView::Template::Error: undefined method `first' for nil:NilClass
from /Users/dannymcclelland/Projects/premvet/app/views/user_mailer/finance_report.text.erb:3:in `_app_views_user_mailer_finance_report_text_erb___4501411113534248604_70358523362460'
It works when I pull a value from the standard method:
<%= number_to_currency(Clinical.first.UserID) %>
but not when I call it from the #clinical_income_by_month report I created.
Any pointers would be appreciated!
You need to do something like this:
in user_mailer.rb
def finance_report(clinical_income_by_month)
#clinical_income_by_month = clinical_income_by_month
... # all you had here before
end
And in the controller call it this way:
UserMailer.finance_report(#clinical_income_by_month).deliver
Clearly your #clinical_income_by_month is nil. That means the select query is not returning values as you think it should. This is a model problem. Instead of checking the views, you should fire up the "rails console" and see whether the query returns the values you want.