I have a start_dt and an end_dt object that are of type datetime.
How can I loop from the start_dt, incrementing by 1 day each time until I reach the end date?
You can use the upto and downto methods on Date and DateTime objects:
start_dt = DateTime.parse('2018-01-01')
end_dt = DateTime.parse('2018-01-15')
start_dt.upto(end_dt) { |date| puts date }
2018-01-01T00:00:00+00:00
2018-01-02T00:00:00+00:00
2018-01-03T00:00:00+00:00
2018-01-04T00:00:00+00:00
2018-01-05T00:00:00+00:00
2018-01-06T00:00:00+00:00
2018-01-07T00:00:00+00:00
2018-01-08T00:00:00+00:00
2018-01-09T00:00:00+00:00
2018-01-10T00:00:00+00:00
2018-01-11T00:00:00+00:00
2018-01-12T00:00:00+00:00
2018-01-13T00:00:00+00:00
2018-01-14T00:00:00+00:00
2018-01-15T00:00:00+00:00
end_dt.downto(start_dt) { |date| puts date }
2018-01-15T00:00:00+00:00
2018-01-14T00:00:00+00:00
2018-01-13T00:00:00+00:00
2018-01-12T00:00:00+00:00
2018-01-11T00:00:00+00:00
2018-01-10T00:00:00+00:00
2018-01-09T00:00:00+00:00
2018-01-08T00:00:00+00:00
2018-01-07T00:00:00+00:00
2018-01-06T00:00:00+00:00
2018-01-05T00:00:00+00:00
2018-01-04T00:00:00+00:00
2018-01-03T00:00:00+00:00
2018-01-02T00:00:00+00:00
2018-01-01T00:00:00+00:00
How about this?
current_date = object.start_dt
end_date = object.end_dt
loop do
## do something
## (`:beginning_of_day` and `:end_of_day` may help you)
current_date += 1.day
break if current_date > end_date
end
Try to the following
start_date = Time.now - 30.day
=> 2017-12-16 09:19:51 +gmt
end_date = Time.now
=> 2018-01-15 09:19:44 +gmt
start_date = Date.parse "#{start_date}"
end_date = Date.parse "#{end_date}"
date_count = (end_date - start_date).to_i
=> 30
<% (1..date_count).each do |i| %>
#Code to display using <%= i %> that you want to display
<% end %>
Hope to help
Try these:
(DateTime.parse('2018-01-01')..DateTime.parse('2018-01-15')).each do |date|
puts date
end
I need to iterate through a database table where it's conditions will change:
<%# if #story.category == 5 %>
<% #users.where(generalLabour: 1).find_each do |user| %>
<% else %>
<% #users.each do |user| %>
.... Iterate and display data.....
So I need to do something like this, unfortunately this does not seem to work as I cannot run statements like this alongside else statements
I also need to do integer comparisons:
<% #users.where(starttime: > 12 ).find_each do |user| %>
How I do something like this?
Maybe I should do all this in Javascript instead of rails?
Try this
query= ""
if #story.category == 5
query = "generalLabour = 1"
elsif #story.category == 4
query = "generalLabour = 2"
elsif #story.category == 6
query = "generalLabour = 3"
end
#users.where(query).each do |user|
.........
end
query= ""
if #story.category == 5
query = "SaturdayS <= #{story.startTime}"
elsif #story.category == 4
query = "SaturdayE >= #{story.endTime}"
end
#users.where(query).each do |user|
.........
end
You can use a hash with mappings:
x = { 5 => 1, 4 => 2, 6 => 3 }[#story.category]
Or use case statement (which is the ruby version of switch):
x = case(#story.category)
when 5
3
when 4
2
when 6
3
else
raise "Doh! I did not think of this."
end
But it is very likely that you are just doing it wrong. Hardcoding IDs in your application is not a good solution and you should instead solve this by setting up a proper association and getting the related items through it.
I currently have the following code:
- #alpha = Glossary.find(:all, :order =>"title ASC").group_by{|u| u.title[0]}
- #glossary = Glossary.find(:all, :order =>"title ASC")
- #alpha.each do|a|
%h1= a[0]
- #glossary.each do |g|
%p display stuff
This displays all of the glossary terms under each letter rather than only the ones that begin with the letter.. I've tried a few things but I'm not sure how to select the right thing.
You should be able to do everything with your #alpha instance variable, since you're using group_by:
- #alpha = Glossary.find(:all, :order =>"title ASC").group_by{|u| u.title[0]}
- #alpha.each do |alpha, glossary_array|
%h1= alpha
- glossary_array.each do |item|
%p= item
You're close. I think you just want to do
- #alpha = Glossary.order("title ASC").group_by{|u| u.title[0]}
- #alpha.each do |letter, items|
%h1= letter
- items.each do |item|
%p= item
It seems that #original_id and comment.id have to be converted to strings to compare equal, when I don't convert them to strings, the string in the else branch is returned. Why is this? And is there a way round it?
- if #original_id.to_s == comment.id.to_s
= "Matched"
- else
= "hi"
Context:
.comment{:class => "c" + nesting.to_s}
.profile
%img{:src => "/assets/profile_image_sample.jpg"}
.message
.username
- if comment.user.username.blank?
= comment.user.first_name
- else
= comment.user.username
= comment.content
.reply-link
= link_to "Reply to comment...", post_path(:original_id => comment.id)
= #original_id.to_s + "and" + comment.id.to_s
- if #original_id.to_s == comment.id.to_s
= "Matched"
- else
= "hi"
- if comment.replies.count > 0
- nesting = nesting + 1
- comment.replies.each do |comment|
= render "comment", :comment => comment, :nesting => nesting
One of those two variables is a string, while the other is a number.
A possible cause is that in the controller, all received (posted) parameters are strings. Without explicit conversion, you are passing this value to the view as a string.
And to answer your second question:
Convert #original_id to an integer when setting it in the controller:
#original_id = params[:original_id].to_i
Here's a sample of array:
{"C1"=>[
{:upc=>"51857195821952", :product_id=>"1234", :name=>"name", :price=>" $15 ", :color=>"green", :size=>"L", :description=>"descr"},
{:upc=>"352353wegs", :product_id=>"456", :name=>"name2", :price=>"$21", :color=>"black", :size=>"S", :description=>"descr"}, # ...
],
#...
}
And here as I am trying to fetch data from that array:
#array.each do |p|
product = Product.new
product.sku = p[0]
product.name = p[1][0][:name] #can't convert Symbol into Integer
price = p[1].select{ |pr| !pr[:price].nil? and pr[:price] != "0" }.min_by{ |i| i[:price].to_f }[:price]
product.price = "%.2f" % (price.to_f)
...
end
Every time I try to fetch data from the array, I get on the line product.name = the error can't convert Symbol into Integer.
What is wrong in this case? I spent a part of afternoon on this issue, but unfortunately I still cannot figure out it...
Thanky you
Your #array is actually a hash. It is formated like following:
{
'name1' => [{:upc => "..."},{:upc => "..."}],
'name2' => [{:upc => "..."},{:upc => "..."}],
#...
}
Since it is a Hash, you can use 2 arguments in the each (works for map also) method (one for the key, the other for the value):
#array.each do |name, array|
product = Product.new
product.sku = name # returns "C1"
array.each do |data|
data[:upc]
data[:name]
#etc...
end
end
The fundamental problem is that the sample array you showed above is not actually an array. It's a hash with key-value pairs. Therefore, your code like p[0] or p[1][0] doesn't make sense because a hash doesn't have index like array. Hash is not ordered. Hashes values are accessed with a "key" rather than an "index" like array.
Iterating through key-value pairs of a hash is done something like this.
1.9.3p194 :001 > x = {:x => 10, :y => 9, :z => 10}
=> {:x=>10, :y=>9, :z=>10}
1.9.3p194 :002 > x.each do |key, value|
1.9.3p194 :003 > puts "#{key} : #{value}"
1.9.3p194 :004?> end
x : 10
y : 9
z : 10
=> {:x=>10, :y=>9, :z=>10}
It looks like you may be confusing Arrays and Hashes a bit.
Given this:
#array = {"C1"=>[
{:upc=>"51857195821952", :product_id=>"1234", :name=>"name", :price=>" $15 ", :color=>"green", :size=>"L", :description=>"descr"},
{:upc=>"352353wegs", :product_id=>"456", :name=>"name2", :price=>" $21 ", :color=>"black", :size=>"S", :description=>"descr"}
] }
Then #array.class.name is Hash
You can get the actual array by accessing it like so:
#actual_array = #array["C1"]
Then, #actual_array.class.name will be Array
So, taking this approach and re-writing:
#array = {"C1"=>[
{:upc=>"51857195821952", :product_id=>"1234", :name=>"name", :price=>" $15 ", :color=>"green", :size=>"L", :description=>"descr"},
{:upc=>"352353wegs", :product_id=>"456", :name=>"name2", :price=>" $21 ", :color=>"black", :size=>"S", :description=>"descr"}
] }
#actual_array = #array["C1"]
#actual_array.each do |p|
puts p[:name]
end
If you do this, you'll find that the value of the :name element will be printed neatly out.