I'd like to create a todo list looking for any cell that matches today's date on either agreement, start or due date.
<% #project.where("project_date_agreement + project_date_start + project_date_due > ?", Date.today).each do |tasks_today| %>
<ul>
<li>Item Due Today</li>
</ul>
<% end %>
Any help getting me in right direction would be appreciated.
You want to use OR conditions in your where clause. Properly speaking this would be in your controller.
#projects = Project.where('date_agreement = ? OR date_start = ? or date_due = ?', Date.today, Date.today, Date.today)
In your Project Model you might want to create a field that say's what's due...
def due_because
due_array = []
due_array << 'Agreement date' if date_agreemnt == Date.today
due_array << 'Start date' if date_start == Date.today
due_array << 'Due date' if date_due == Date.today'
due_array.join(', ')
end
Then in your view you would iterate over the #projects
<ul>
<li>Items Due Today</li>
<% #projects.each do |project| %>
<li><%=project.name%> <%=project.due_because%></li>
<% end %>
</ul>
if you asking one row and different column, how about if you combine with table
<% #tasks = #projects.where("date_agreement = ? AND date_start = ? AND date_due = ?",Date.today ,Date.today, Date.today) %>
<table>
<tr>
<ul>
<% #tasks.each do |task| %>
<td>
<li><%= task.check_box :item_due %></li>
</td>
<% end %>
</ul>
</tr>
</table>
How to trigger conditional with #future_challenges if deadline is not equal to Date.current.year.month?
controller
#future_challenges = current_user.challenges.unaccomplished.order("deadline ASC").select{ |challenge| challenge.deadline > Date.current if challenge.deadline.present? }
view
<% if #future_challenges != Date.current.year.month %> # Is giving true even if there are challenges with deadline in current month of this year
<div style="margin-top: -4px;"></div>
<% end %>
Unless I have misunderstood then in the controller all you would need to do is something like:
#future_challenges = current_user.challenges.where("deadline >= ?", Date.current.at_beginning_of_month)
and then in the view just check the number of entries:
<% if #future_challenges.empty? %>
<div style="margin-top: -4px;"></div>
<% end %>
The code below shows number of appointment each hour and 'No appointment' if no appointment is available.
I want to group consecutive 'No appointment' slot according to time
for e.g., The output of the current code is -
10:00 AM to 11:00 AM -> No Appointment
11:00 AM to 12:00 PM -> No Appointment
12:00 PM to 01:00 PM -> No Appointment
I want the output to be
10:00 AM to 01:00 PM -> No Appointment
The code is give below:-
<% #time_group = ['6:00:00','7:00:00','8:00:00','9:00:00','10:00:00','11:00:00','12:00:00','13:00:00','14:00:00','15:00:00','16:00:00','17:00:00','18:00:00','19:00:00','20:00:00','21:00:00','22:00:00','23:00:00'] %>
#Loop through each hour in #time_group
<% #time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% #booked = Appointment.where(start_time: t1..t2 %>
#Show time interval e.g 1:00 PM to 2:00 PM
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
#if appointment is available show appointment else show no appointment
<% if #booked.size > 0 %>
<% #booked.each do |app| %>
<%= app.start_time.strftime('%I:%M') %>
<% end %>
<% else %>
No Appointment
<% end %>
<% end %>
First of all, one might prepare the array:
#time_group = ['6:00:00','7:00:00', ...]
#stacked = #time_group.each_with_object([]) do |start, memo|
range = Time.parse(start)..Time.parse(start)+3600
booked = Appointment.where(start_time: range)
if memo.empty? || !booked.empty? || !memo.last.last.empty?
memo << [range, booked] # no sequenced empty slots
else
memo << [memo.pop.begin..range.end, booked] # merge ranges
end
end.to_h # { ranges => booked }
And now—let’s output them:
<% #stacked.each do |range, booked| %>
.....
<% end %>
Try like this
<% #time_group = ['6:00:00','7:00:00','8:00:00','9:00:00','10:00:00','11:00:00','12:00:00','13:00:00','14:00:00','15:00:00','16:00:00','17:00:00','18:00:00','19:00:00','20:00:00','21:00:00','22:00:00','23:00:00'] %>
#Loop through each hour in #time_group
<% #time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% #booked = Appointment.where(start_time: t1..t2) %>
#It will holds the No Appointment Timings into it
no_appointment_array = []
#if appointment is available show appointment else show no appointment
<% if #booked.size > 0 %>
#Calling the Function
display_no_appointment(no_appointment_array) if not no_appointment_array.empty?
#Show time interval e.g 1:00 PM to 2:00 PM
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
<% #booked.each do |app| %>
<%= app.start_time.strftime('%I:%M') %>
<% end %>
<% else %>
no_appointment_array << [Time.parse(t).strftime('%I:%M %p'), (Time.parse(t) + 3600).strftime('%I:%M %p')]
<% end %>
<% end %>
def display_no_appointment(no_appointment_array)
puts "#{no_appointment_array.first[0]} to #{no_appointment_array.last[1]} -> No Appointment"
#It Clears all Previous No Appointment
no_appointment_array.clear
end
First of all I suggest you to use if '#booked.present?' instead of 'if #booked.size > 0'
Have you tried something like this?
I have not test this code, it is almost an idea to a possible solution.
Hope that it could be helpful for you.
<% no_appointment_starts_at = nil %>
<% no_appointment_ends_at = nil %>
<% #time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% #booked = Appointment.where(start_time: t1..t2 %>
#Show time interval e.g 1:00 PM to 2:00 PM
#if appointment is available show appointment else show no appointment
<% if #booked.present? %>
<% #booked.each do |app| %>
<% unless no_appointment_ends_at.blank? %>
<%= Time.parse(no_appointment_starts_at).strftime('%I:%M %p') %> to <%= (Time.parse(no_appointment_ends_at) + 3600).strftime('%I:%M %p') %>
<% no_appointment_ends_at = nil%>
<% end %>
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
<%#= app.start_time.strftime('%I:%M') %>
<% no_appointment_starts_at = t2 %>
<% end %>
<% else %>
<% last_appointment_ends_at = t2 %>
<% end %>
<% end %>
I have a rooms model that looks like follows:
class Room < ActiveRecord::Base
belongs_to :host
has_many :bookings
def space
if self.bookings.empty?
return self.capacity
else
return (self.capacity - total_booked)
end
end
def total_booked
total_booked = 0
room.bookings.each { |booking| total_booked += booking.number_of_guests if
((booking.start_date >= #start_date && booking.start_date <= #end_date) ||
(booking.end_date >= #start_date && booking.end_date <= #end_date)) }
return total_booked
end
end
In my view I'm trying to call on these methods like so:
<% host.rooms.each do |room| %>
<!-- display information on free rooms -->
<% if room.bookings.empty? %>
<p>room #<%= room.id %> is available (0 booked, <%= room.capacity %> free out of <%= room.capacity %>)</p>
<% else %>
<% if !(room.space == 0) %>
<p>room #<%= room.id %> is available (<%= room.total_booked %> booked, <%= room.space %> free out of <%= room.capacity %>)</p>
<% end %>
<% end %>
<% end %>
Unfortunately this gives me the error: undefined local variable or method 'room' for the following line:
<% if !(room.space == 0) %>
I think there might be a syntax error somewhere since I've called on model methods from a view file before but I can't spot it. Any ideas?
Edit:
full error log:
undefined local variable or method `room' for #<Room:0x00000003a714f0>
Extracted source (around line #25):
22 <% if room.bookings.empty? %>
23 <p>room #<%= room.id %> is available (0 booked, <%= room.capacity %> free out of <%= room.capacity %>)</p>
24 <% else %>
25 <% if !(room.space == 0) %>
26 <p>room #<%= room.id %> is available (<%= room.total_booked %> booked, <%= room.space %> free out of <%= room.capacity %>)</p>
27 <% end %>
28 <% end %>
def total_booked
total_booked = 0
room.bookings.each { |booking| total_booked += booking.number_of_guests if
((booking.start_date >= #start_date && booking.start_date <= #end_date) ||
(booking.end_date >= #start_date && booking.end_date <= #end_date)) }
return total_booked
end
room is not defined in this context. You probably meant self.bookings or just bookings.
In code:
<% #line_item.each do |line_item| %>
<% id=line_item.menu_id%>
<% #menu1=Menu.find(id)%>
<%= #menu_name = #menu1.menu_item_name%>
<% #offers.each do |offer| %>
**<% if offer.menuName_buy1 == #menu_name && offer.menuName_buy2 == #menu_name %>**
I am new in rails.I want to compare menuName_buy1 & menuName_buy2 with #menu_name, but how do i achieve this result.At a time i am getting 1 value for #menu_name.