aes = Model.find_all_by_id(ids, :include => {:historic_task_allocation => {:raw_allocation_incident_links => {:incident => [{:incident_type => :incident_category}, :incident_priority]}}}, :conditions => conditions)
when condition add it take a lot time but i removed it Quickly executed
conditions = ["(allocation_incident_links.valid_from IS NULL
OR allocation_incident_links.valid_from < action_events.date)
AND (allocation_incident_links.valid_to >= action_events.date
OR allocation_incident_links.valid_to IS NULL)
AND incidents.confirmed_at >= ?
AND incidents.confirmed_at < ?
AND ifr94.field_type = ?
AND ifr94.field_id = itusf94.id
AND itusf94.incident_user_standard_field_id = ?
AND (incidents.status_code = ? OR incidents.status_code = ? OR incidents.status_code = ?)
AND (incidents.incident_type_id = ?)
AND ifr94.incident_id = incidents.id",
Fri, 01 Jan 2010, Wed, 01 Jun 2016, "IncidentTypeUserStandardField", "94", 1, 4, 2, 2]
Note - Indexes already added in required tables
Related
I've got in my TravelIdeas table field country_trips, which includes the array of countries.
#<TravelIdea:0x007faa7bec40f0> {
:id => 9,
:idea_key => "romantic",
:idea_type => nil,
:cost => 2000,
:created_at => Mon, 10 Jul 2017 07:48:49 UTC +00:00,
:updated_at => Mon, 16 Oct 2017 08:10:47 UTC +00:00,
:country_trips => [
[0] "PL"
],
:available_languages => [
[0] "pl"
]
}
Local travels has nil in :country_trips, so I can ignore them:
idea.where.not(country_trips: nil) if country.present?
I've got my user destination country, so I want to show him all travel ideas which contains his dreamed country.
It would be sth like SQL server CONTAINS (column, 'country')
Probably this is not about LIKE because this is not about matching 1 country to 1 country.
For example
country = user.destination_country
means:
country = "PL"
but
:country_trips => [
[0] "PL",
[1] "DE"
],
I'm looking for sth like
TravelIdea.where(country_trips: country)
that will return all travel ideas which have this country in country_trips array :P
This is more a postgres question, section 8.15.5. Searching in Arrays says:
SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter);
In your case, you could write that as a SQL segment inside the .where method:
TravelIdea.where("? = ANY (country_trips)", country)
I have an array and a hash. The array contains dates and the hash contains a date as key and a float as value (revenue_green).
This is my array:
#reporting_dates = #month_ends.find_all {|e| e < #current_reporting_date.to_s } <<
#current_reporting_date
And my hash:
#revenue_green = #reports_at_reporting_dates.sum('revenue_green')
Which gives me the following output:
#reporting_dates: ["2017-01-27", "2017-02-24", Fri, 10 Mar 2017]
#revenue_green: {Fri, 10 Mar 2017=>7.0}
For two dates of #reporting_dates (2017-01-27 and 2017-02-24) #revenue_green does not have any values.
I want to create a hash that gives me the following output:
#new_hash: {2017-01-27 => 0, 2017-02-24 => 0, 2017-03-10 => 7.0}
so for all dates where no revenue_green exists it should put a 0.
How can I do that?
UPDATE
#month_ends = ["2017-01-27", "2017-02-24", "2017-03-31", "2017-04-28",
"2017-05-26", "2017-06-30", "2017-07-28", "2017-08-25",
"2017-09-29", "2017-10-27", "2017-11-24", "2017-12-29"]
#all_dates = (Date.new(#reporting_year).
beginning_of_year..1.month.from_now).
to_a.reverse.select { |day| day.wday == 5 }
#current_reporting_date = #all_dates.select { |d| d <= Date.today }.first
#all_reports = Report.all
#reports_at_reporting_dates = #all_reports.where(:day => #reporting_dates).order(:day)
Assuming your starting objects are
#reporting_dates = ["2017-01-27", "2017-02-24", "Fri, 10 Mar 2017"]
#revenue_green = {"Fri, 10 Mar 2017"=>7.0}
(i.e., quotes around "Fri, 10 Mar 2017"), then this should work:
require 'date'
#new_hash = Hash.new
#reporting_dates.each {|d| #new_hash[Date.parse(d)] = #revenue_green[d] || 0}
#new_hash => {#<Date: 2017-01-27 ((2457781j,0s,0n),+0s,2299161j)>=>0, #<Date: 2017-02-24 ((2457809j,0s,0n),+0s,2299161j)>=>0, #<Date: 2017-03-10 ((2457823j,0s,0n),+0s,2299161j)>=>7.0}
or, if you want the keys in the new hash to be strings,
#reporting_dates.each {|d| #new_hash[Date.parse(d).strftime("%Y-%m-%d")] = #revenue_green[d] || 0}
#new_hash => {"2017-01-27"=>0, "2017-02-24"=>0, "2017-03-10"=>7.0}
I have something like this:
good_attrs = %w(firm_size priority_level)
good_attrs.each do |attr|
if (score.send(attr) > max.send(attr))
max.send("#{attr}=", score.send(attr))
end
end
What happens, though, is that occassionally it may come across a Max record that looks like this:
#<Max:0x007fe01024b240> {
:id => 2,
:user_id => 1,
:firm_size => 101.0,
:priority_level => nil,
:created_at => Fri, 23 Nov 2012 01:55:53 UTC +00:00,
:updated_at => Fri, 23 Nov 2012 01:58:16 UTC +00:00
}
i.e. max.priority_level = nil.
So how do I modify my initial if statement to handle nil cases on both sides of the evaluation? i.e. if a score or max attribute is nil. Ideally, I would like it to be treated as 0 and proceed accordingly.
You can't compare nil with a Float.
In your case you can take advantage of the fact that nil.to_f == 0.0:
good_attrs = %w(firm_size priority_level)
good_attrs.each do |attr|
if score.send(attr).to_f > max.send(attr).to_f
max.send("#{attr}=", score.send(attr))
end
end
You can overload the attr_reader for priority_level and firm_size in Max
def priority_level
read_attribute(:priority_level).nil? ? 0 : super
end
You can alternatively set default values in an after_initialize block
after_initialize do
self.priority_level = 0 if self.priority_level.nil?
end
I have this problem. i think it is simple but i just couldn't get it right.
Im building graph and table using gruff.I have a x axis scaled from month 01 to 12
The data shud hv bn plotted in month 09 and 10 but it is plotted to month 08 and 10.
def stat1
#yeara = params[:year]
#games = Games.find(:all)
g = Gruff::StackedBar.new('800x450')
g.theme = {
:colors => ['#138F6A','#330000','#ff6600', '#3bb000', '#1e90ff', '#efba00', '#0aaafd'],
:marker_color => '#aaa',
:background_colors => ['#eaeaea', '#fff']
}
g.hide_title = true
#dr = Game.count(:all, :conditions=> ["game_id= ? and DATE_FORMAT(date, '%Y')= ?",1,#yeara], :group => "DATE_FORMAT(date, '%m')", :order =>"date ASC")
#df = Game.count(:all, :conditions=> ["game_id= ? and DATE_FORMAT(date, '%Y') = ?",2,#yeara], :group => "DATE_FORMAT(date, '%m')", :order =>"date ASC")
# all 12 month a year
#full = Hash["01",0,"02",0,"03",0,"04",0,"05",0,"06",0,"07",0,"08",0,"09",0,"10",0,"11",0,"12",0]
year = (#dr.keys |#df.keys|#full.keys).sort
#keys = Hash[*year.collect {|v| [year.index(v),v.to_s] }.flatten]
# Plot data to table
#dfdr = Array.new
#dfdr << #keys.collect {|k,v| #df[v].nil? ? 0 : #df[v]}
#dfdr << #keys.collect {|k,v| #dr[v].nil? ? 0 : #dr[v]}
# Plot data to graph
g.data("Adam", #keys.collect {|k,v| #dr[v].nil? ? 0 : #dr[v]})
g.data("Eve", #keys.collect {|k,v| #df[v].nil? ? 0 : #df[v]})
g.labels = #keys
g.write("#{RAILS_ROOT}/public/images/game.png")
render(:layout => false)
here are some explanation
the output for
#dr = Game.count(:all, :conditions=> ["game_id= ? and DATE_FORMAT(date, '%Y')= ?",1,#yeara], :group => "DATE_FORMAT(date, '%m')", :order =>"date ASC")
=> [["09",3],["10",1]
#df = Game.count(:all, :conditions=> ["game_id= ? and DATE_FORMAT(date, '%Y') = ?",2,#yeara], :group => "DATE_FORMAT(date, '%m')", :order =>"date ASC")
=> [["10",2]]
trying to trace where went wrong..but ran out of idea.
please point something out to me.wish i could paste the graph but im not eligible yet say stackoverflow. ;)
thank u
OK got it.
#keys.collect {|k,v| #df[v].nil? ? 0 : #df[v]
the #keys are in unsorted order.So simply sort it first.
#keys.sort.collect {|k,v| #df[v].nil? ? 0 : #df[v]
Thank you all. :))))) Sorry if anything. ;)
I want to find all records, say Posts, created today with Ruby on Rails, then all Posts created yesterday, and so on… how should I do?
Thank you,
Kevin
Try this:
#Today
Posts.find(:all, conditions: { :created_at => Date.today...Date.today + 1 })
#Yesterday
Posts.find(:all, conditions: { :created_at => Date.today - 1...Date.today })
Or this (preferable, in my opinion):
#Today
Posts.find(:all, conditions: ["DATE(created_at) = ?", Date.today] )
#Yesterday
Posts.find(:all, conditions: ["DATE(created_at) = ?", Date.today - 1] )
As a rule I store all dates on my server in UTC timezone and let the UI handle any timezone conversion.
To get the sort of query you are after to work correctly I had to massage the incoming date into a
UTC specific time range first.
require 'date'
class Post < ActiveRecord::Base
def self.created(a_date)
return Post.where(created_at: to_timerange(a_date))
end
private
def self.to_timerange(a_date)
raise ArgumentError, "expected 'a_date' to be a Date" unless a_date.is_a? Date
dts = Time.new(a_date.year, a_date.month, a_date.day, 0, 0, 0).utc
dte = dts + (24 * 60 * 60) - 1
return (dts...dte)
end
end
This then allows you to call
# today
posts = Post.created(Date.today)
# yesterday
posts = Post.created(Date.today - 1)
To query using a range I prefer the following:
yesterday = Date.yesterday
start = yesterday.beginning_of_day
#Fri, 27 Nov 2020 00:00:00 UTC +00:00
end = yesterday.end_of_day
# Fri, 27 Nov 2020 23:59:59 UTC +00:00 - the value here is one second before midnight
# meaning we should use an inclusive range using two dots:
range = start..end
Post.where(created_at: range)