Skip to content

Instantly share code, notes, and snippets.

@jonkgrimes
Last active August 29, 2015 14:13
Show Gist options
  • Save jonkgrimes/401bb0345d1c29338596 to your computer and use it in GitHub Desktop.
Save jonkgrimes/401bb0345d1c29338596 to your computer and use it in GitHub Desktop.
Get a list of businesses and ads counts
range = Date.new(2014,12,1)..Date.new(2014,12,31)
# Gets the list of businesses that ran ads in "range" and the ad count
Business.joins(:ads).where(ads: {publisher_id: 365, start_date:range}).uniq.map do |business|
"#{business.name}, #{business.ads.where(publisher_id: 365, start_date: range).count}"
end.each { |elem| puts elem }; 1
# Gets the list of ads that ran for a given publisher with the business name, business uuid
Business.joins(:ads).where(ads: {publisher_id: 365, start_date:range}).uniq.each do |business|
puts "#{business.uuid},#{business.name},#{business.custom_id}"
end
# Gets the list of ads that ran for a given publisher with the business name, business uuid and ad file name
CSV.open("/home/<user>/businesses.csv", "wb") do |csv|
Business.joins(:ads).where(ads: {publisher_id: 575, start_date:range}).each do |business|
business.ads.where(publisher_id: 575, start_date:range).each do |ad|
csv << [business.uuid,business.name,business.custom_id,ad.pdf_file_name]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment