In my database I have column :inviter and some users have same content in that column
I want find all them,with same content
In my user controller:
def foll
#user = current_user
#ref = User.find_by_invite(#user.id.to_s)
if !#ref || !#ref.pay_s || !#ref.pay_1 || !#ref.pay_2 || !#ref.pay_3
#referals = false
else
#referals = User.find_by_invite(#user.id.to_s)
end
end
In my views
<% if #referals != false %>
<% #referals.each do |user| %>
<h4>
<%= link_to user.name, user %> status : Good
</h4>
<% end %>
<% else %>
<h3>You dont have any referrals</h3>
<% end %>
When I try I have this error
undefined method `each' for #<User:0x007f13e41b0068>
this will raise error since
#referals = User.find_by_invite(#user.id.to_s)
will return single record
you can try #where to query all referrals
#referals = User.where(invite: #user.id.to_s)
you can also refer here for other queries
http://guides.rubyonrails.org/active_record_querying.html
hope this helps
Hope this help:
controller:
def foll
#user = current_user
#ref = User.find_by_invite(#user.id.to_s)
if !#ref || !#ref.pay_s || !#ref.pay_1 || !#ref.pay_2 || !#ref.pay_3
#referals = []
else
#referals = User.where(invite: #user.id.to_s)
end
end
view:
<% if #referals.any? %>
<% #referals.each do |user| %>
<h4>
<%= link_to user.name, user %> status : Good
</h4>
<% end %>
<% else %>
<h3>You dont have any referrals</h3>
<% 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 %>
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.
Hi newb to Rails here.
I want to format a string to fit into an html div. The model I'm after is;
Break the string into an array seperated by whitespace.
If any array item length is greater than 22
Split the characters into lengths of 22 and display.
else separate the words by whitespace and then display.
It's for handling long names on a guestbook type application.
<% $p = 0 %>
<% #m = name.split(" ") %>
<% while $p < #m.size do %>
<% if #m[$p].length > 22 %>
<%= name.slice(0, 21) %><br>
<%= name.slice(21, 43) %><br>
<% else %>
<% $i = 0 %>
<% #x = name.split(" ") %>
<% while $i < #x.size do %>
<%= #x[$i] %><br>
<% $i +=1 %>
<% end %>
<% end %>
<% $p +=1 %>
<% end %>
(1..(name.length / 22)).each { |i| name[22*i] = ' ' } if name.length > 22
My problem is there are 160 entries published at today but i see only 13 entries when i print entries_count. I guess it is about timezone. My timezone is Athens. Any help will be appreciated. I spent my whole day to fix this.
#from_date = Time.zone.now.beginning_of_day
#to_date = Time.zone.now.end_of_day
#entries_by_date = Entry.where(:published_at => #from_date..#to_date).group("date(published_at)").select("date(published_at) as date, count(*) as entries_count")
view
% (#from_date.to_date..#to_date.to_date).each do |day| %>
<% a= #entries_by_date.detect {|entry| entry.date == day} %>
<% if a %>
<%= a.entries_count %>
<% else %>
0
<% end %>
<% end %>
Update:
Time.zone.now can be any date Time.zone.now is for example.
You just want to match on day/month/year. Try this:#entries_by_date = Entry.where(:published_at => #from_date..#to_date).
group("year(published_at), month(published_at), day(published_at)").
select(" year(published_at) as year,
month(published_at) as month,
day(published_at) as day,
count(*) as entries_count")
then the view:
<% (#from_date.to_date..#to_date.to_date).each do |date| %>
<% a= #entries_by_date.detect {|entry| entry.day == date.day &&
entry.month == date.month &&
entry.year == date.year} %>
<%= a.try(:entries_count) || 0 %>
<% end %>