how to compare two conditions in each loop - ruby-on-rails

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.

Related

How find users in database with same content in Ruby on Rails?

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 %>

How can I get the model name of object in rails?

I got four different models.
Here's an example,
#single = Single.all
#coe = Coe.all
#blend = Blend.all
#production = #single+#coe+#blend
then how to check which model #production is?
I tried
<% #production.each do |p| %>
<%=p.class.name%>
<% end %>
but it returns "Array"
It seems to be simple, but I can't find out
(I edited question)
The problem is here
#single = Single.all
#coe = Coe.all
#blend = Blend.all
#production = #single+#coe+#blend
change these lines with
#single = Single.all.to_a
#coe = Coe.all.to_a
#blend = Blend.all.to_a
#production = #single+#coe+#blend
and then if you will check
#production.first.class.name #Single
#production.last.class.name #Blend
so in your view you can do this
<% #production.each do |p| %>
<% p.each do |product| %>
<%= product.class.name %>
<% end %>
<% end %>
if while iterating on #production it returns array so you need to try this.
<% #production.each do |p| %>
<% p.each do |product| %>
<%= product.class.name %>
<% end %>
<% end %>
#production is a collections of combinations of single, coe, and blend thats why #production.class.name doesnt work, you need to iterate each object like this:
<% #production.each do |object| %>
<%= object.class.name %>
<% end %>

Custom method in model - undefined local variable

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.

Basic string formatting in Rails view using loops

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

Timezone and group

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 %>

Resources