Find users created during a specific hour range (any day) - mysql

For fun, I'd like to see the set of users (in a Rails database) who were created during a specific hour range (2AM - 5AM to be specific), but on any day. They all have the typical created_at field. I think I know how to extract the hour from this field for one user, and then see if it falls in a range--but how I do do this for all of them? Should I just loop through them? (Even as I write it, that sounds naive).

The first part of Sontyas answer is the easy solution in rails.
I would however move that part to it's own place inside your class to separate your code from the framework a bit more.
# app/models/user.rb
class User < ActiveRecord::Base
# ...
def self.get_users_created_between(start_time, end_time)
User.where("TIME(created_at) BETWEEN TIME(?) AND TIME(?)", start_time, end_time)
end
# ...
end
And use it like this:
irb> User.get_users_created_between(Time.parse("2pm"), Time.parse("5pm"))
This provides you with a couple of benefits:
You can reuse it all over your code without ever having to worry about the syntax of the where or time ranges.
If for some weird reason rails decides to change the interface for this, you only need to edit one method and not code in a thousand places all over your project.
You can easily move this piece of code out of the user.rb if you feel that user.rb gets to big. Maybe to some dedicated finder or query class. Or to something like a repository pattern.
PS: Time functions may vary between different DBMS like MySQL, Postgresql, MSSQL etc. I don't know, if there is generic way to do this. This answer is for MySQL.

Try this,
User.where(created_at: Time.parse("2pm")..Time.parse("5pm"))
Or something like this
User.select { |user| user.created_at.hour.between?(2, 5) }

To return users that where created between two hours on any given day, use this:
User.where('HOUR(created_at) BETWEEN ? AND ?', 2, 5)
Please note that HOUR(created_at) only works for MySQL. The syntax in Postgresql is extract(hour from timestamp created_at) and strftime('%H' created_at) in SQLite.

Related

Query ActiveRecord for records and relation calculations at once

TL;DR? See Edit 2
I've got a little Rails application that has a few different sort of games people can play: it's based around sports, so they can pick the winners of each game every week (model PickEm, attribute correct boolean with nil for unfinished games), and predict the outcome of a specific team's game (model Guess, attribute score with integer, nil for unfinished games). Every User has_many PickEms and Guesses. And I'm trying to display standings (correct/total - total being all non-nil, score/total possible).
What I'm finding is that I can gather the users and their associated records, but in trying to display standings I'm discovering that every single User is triggering another query - slow and not sustainable as the user base increases. That's because #user.pick_em_score is pick_ems.where(correct: true).size and #user.guess_Score is guesses.where.not(score: nil).sum(:score). So I call user.pick_em_score and it runs that query. I feel like there should be a way to get every User, as well as these specific counts, at once, rather than buffering a whole bunch of needless extra stuff.
What I need:
User record
User.pick_em_score (calculated by counting correct records)
User.pick_ems count where NOT NULL
User.guesses_score (calculated by guesses.sum(:score))
User.guesses count where NOT NULL
Most of the stuff I find on Rails's ActiveRecord helpers, especially related to calculations, is for retrieving only the calculation. It looks like I'll probably need to delve directly into select() etc. But I can't get it working. Can someone point me in the right direction?
Edit
For clarification: I'm aware that I can write this information to the User model, but this is overly restrictive: next season, I'll need to add a new column to the User for that year's results, etc. In addition, this is a third degree of callback updating related models – the Match model already updates related PickEms and Guesses on save. I'm looking for the simplest ActiveRecord query or queries to be able to work with this information, as indicated by the title. Ideally one query that returns the above information, but if it needs to a few, that's OK.
I used to work directly in MySQL with PHP, but those skills have rusted (in raw MySQL, I imagine, I'd have several sub-select statements to help pull these counts) and I'd also like to be able to use Rails's ActiveRecord helpers and such, and avoid constructing raw SQL as much as possible.
Second Edit:
I seem to have it down to one call that starts to work, but I'm writing a lot of SQL. It's also brittle, IMO, and trying to run with it has failed. It also looks like I'm just pushing the million singular SELECT queries from Rails right into SQL, but that may still be a step up.
User.unscoped.select('users.*',
'(SELECT COUNT(*) FROM pick_ems WHERE pick_ems.user_id = users.id AND pick_ems.correct) AS correct_pick_ems',
'(SELECT COUNT(*) FROM pick_ems WHERE pick_ems.user_id = users.id AND pick_ems.correct IS NOT NULL) AS total_pick_ems',
'(SELECT SUM(guesses.score) FROM guesses WHERE guesses.user_id = users.id AND guesses.score IS NOT NULL) AS guesses_score',
'(SELECT COUNT(*) FROM guesses WHERE guesses.user_id = users.id AND guesses.score IS NOT NULL) AS guesses_count' )
The issue seems to be: is there a way to use Rails, and not raw SQL, to link up users.id that we see there with these subqueries? Or just … a better way to construct this, in general?
In addition, I'm running another set of SELECTs for the WHERE, which would hinge on total_pick_ems and guesses_count being > 0 but since I can't use those aliased columns, I have to call the SELECT one more time.
Welcome to AR. Its really only good for simple CRUD like queries. Once you actually want to query your data in anger it just doesn't have the capababilities to do the queries you want without resorting to wholesale SQL strings and often abandoning the ability to chain as a result.
Its precisely why I moved to Sequel as it does have the features to compose queries using a much fuller SQL feature set, including join conditions, window functions, recursive common table expressions, and advanced eager loading. The author is incredibly responsive and documentation is excellent compared to AR and Arel.
I don't expect you will like this answer but a time will come when you will start to look outside the opinionated components that come with rails which I have to say are hardly best of breed. Sequel also sped my application up many times over what I was able to get with AR as well, it not just developer happiness, it means less servers to run. Yes it will be a learning curve but IMO its better to learn tools that have your back covered.
Joins might work. Smthing like below
User.unscoped.joins(:guesses).joins(:pick_ems).
where("guesses.score IS NOT NULL").
select("users.*,
sum(guesses.score) as guesses_score,
count(guesses.id) as guesses_count,
count(case when pick_ems.correct = True then 1 else null end)
as correct_pick_ems,
count(case when pick_ems.correct != null then 1 else null end)
as total_pick_ems,
").
group("users.id")
If you need this information for a limited number of users at a time then above query or eager loading (User.includes(:guesses, :pick_ems)) with class methods like
def correct_pick_ems
pick_ems.count(&:correct)
end
would work.
However If you need this information for all the users most of the time, cached counters within the users table would be more optimal.
What you need is some sort of custom (smart) counter_cache to count only at certain conditions (e.g correct is true)
You can achive this using conditional after_save & after_destroy triggers to build your own custom counter_cache that looks like this:
class PickEm
belongs_to :user
after_save :increment_finished_counter_cache, if: Proc.new { |pick_em| pick_em.correct }
after_destroy :decrement_finished_counter_cache, if: Proc.new { |pick_em| pick_em.correct }
private
def increment_finished_counter_cache
self.user.update_column(:finished_games_counter, self.user.finished_games_counter + 1) #update_column should not trigger any validations or callbacks
end
def decrement_finished_counter_cache
self.user.update_column(:finished_games_counter, self.user.finished_games_counter - 1) #update_column should not trigger any validations or callbacks
end
end
Notes:
Code not tested (only to show the idea)
Some guys said it's better to avoid naming custom counters as rails name them (foo_counter_cache)
You should benchmark it, but my hunch is that adding all of that data into a single SELECT isn't going to be much faster than breaking it up into separate SELECTs (I've actually had cases where the latter was faster). By breaking it up, you can also stick to more ActiveRecord and less raw SQL, e.g.:
user_ids_to_pick_em_score = User.joins(:pick_ems).where(pick_ems: {correct: true}).group(:user_id).count
user_ids_to_pick_ems_count = User.joins(:pick_ems).where.not(pick_ems: {correct: nil}).group(:user_id).count
user_ids_to_guesses_score = Hash[User.select("users.id, SUM(guesses.score) AS total_score").joins(:guesses).group(:user_id).map{|u| [u.id, u.total_score]}]
user_ids_to_guesses_count = User.joins(:guesses).where.not(guesses: {score: nil}).group(:user_id).count
Edit: To display them, you could do like so:
<%- User.select(:id, :name).find_each do |u| -%>
Name: <%= u.name %>
Picks Correct: <%= user_ids_to_pick_em_score[u.id] %>/<%= user_ids_to_pick_ems_count[u.id] %>
Total Score: <%= user_ids_to_guesses_score[u.id] %>/<%= user_ids_to_guesses_count[u.id] %>
<%- end -%>

Propel 1.6 Greater / Less than

Im trying to do something pretty simple in Propel ORM which I just cant seem to work out or find the answer to.
Basically I have a table called post and a table called post_history. Every time someone makes an edit to a post, the previous version is saved to post_history first.
I have an 'undo' button against each post where I simply want to select the last 'post_history' record for the post and revert the post data from the history. To select the last history that is older than my post how do I select GREATER or LESS than in propel?
If I do something like this:
$postHistory = PostHistoryQuery::create()
->filterByPostId($post->getId())
->filterByCreated(array("max" => $post->getUpdated()))
->orderById("DESC")
->limit(1);
the filterByCreated with the 'max' specified does indeed select the last 'post_history' but if you click undo again it selects the same record because the max seems to specify less or equal to rather just less than. It may require a custom query? I think im missing a trick that I cant find the answer too. My search for the answer hasn't helped so far.
Any help is much appreciated.
Ok, so I posted a question on SO for help and within 30 minutes I work it out by just trying things. Typical, eh? It might be useful to someone else so here's what I did:
$postHistory = PostHistoryQuery::create()
->filterByPostId($post->getId())
->filterByCreated(array("max" => $post->getUpdated()))
->orderById("DESC")
->limit(1);
Changed to:
$postHistory = PostHistoryQuery::create()
->filterByPostId($post->getId())
->where('post_history.created < ?', $post->getUpdated())
->orderById("DESC")
->limit(1);
The ->where() clause allows you to specify lots of options e.g >, <, LIKE etc...

Exception ActiveRecord::StatementInvalid doesn't works

I'm relative new to Rails, so I don't know if my way to solve the problem is the correct, but there's some problem with it
I have installed in my PC MySQL, but Heroku uses PostgreSQL, so I'm designing a solution to work with both DBMS in certain problem.
I have the next code:
begin
#products_with_valid_offers = Product.joins(:variants).active.discount_valid.discount_date_valid_mySQL
rescue ActiveRecord::StatementInvalid
#products_with_valid_offers = Product.joins(:variants).active.discount_valid.discount_date_valid_postgreSQL
end
And the scopes are:
scope :active, includes([:assets, :store]).where(:active => true, :deleted_at => nil, :stores => { :deleted_at => nil, :active => true }).order("products.created_at DESC")
scope :discount_date_valid_mySQL, where('DATE_FORMAT(NOW(),"%Y-%m-%d 23:59:59") + '" BETWEEN discount_from AND discount_to')
scope :discount_date_valid_postgreSQL, where('now()::date BETWEEN discount_from AND discount_to')
As you see, I need 2 different forms to manage the date format, one with each DBMS.
The issue is that the flow never enters in the exception. If #products_with_valid_offers is a SQL Error in MySQL, never enters to the rescue block to execute the PostgreSQL line, and it returns an error.
Some help, please?. :D
Your quotes are messed up in discount_date_valid_mySQL, you want to say this:
scope :discount_date_valid_mySQL, where("DATE_FORMAT(NOW(),'%Y-%m-%d 23:59:59') BETWEEN discount_from AND discount_to")
That at least is valid Ruby code.
That said, your current approach is bad in various ways:
You're ensuring that you always have an exception in production.
You're developing on top of one database but deploying on top of another, this alone will cause you all sorts of problems.
You should be using just date_format(now(), '%Y-%m-%d') in your MySQL version.
There are probably other issues but there's no need to spend more time picking apart your code: there is a better way. Both MySQL and PostgreSQL (and even SQLite) support current_date so you can just use one thing for both:
scope :discount_date_valid, where('current_date between discount_from and discount_to')
Assuming of course that you want everything to assume UTC. If you want things to use some other time zone then:
Store your discount_from and discount_to as date columns, dates don't have timezones in SQL. Presumably you're doing this already but I just want to make sure.
Adjust your scope to get the current date from your client code (which is presumably configured to use the right timezone):
def self.discount_date_valid
where(':current_date between discount_from and discount_to', :current_date => Date.today)
end
You have to use a class method for the scope to ensure that Date.today is evaluated at the right time (i.e. when the scope is used instead of when the class is loaded).
And you really should develop on top of PostgreSQL if you're deploying on top of PostgreSQL. You should even make sure you're developing and deploying on the same version of PostgreSQL. Developing on top of one stack and deploying on another causes all sorts of pointless frustration and confusion.

How to find all database rows with a time AFTER a specific time

I have a "last_action" column in my users table that updates a unix timestamp every time a user takes an action. I want to select all users in my users table who have made an action on the site after a specific time (probably 15 minutes, the goal here is to make a ghetto users online list).
I want to do something like the following...
time = Time.now.to_i - 900 # save the timestamp 15 minutes ago in a variable
User.where(:all, :where => :last_action > time)
What's the best way to do this? Is there a way of doing it without using any raw SQL?
Maybe this will work?
User.where("users.last_action > ?", 15.minutes.ago)
Try this:
time = Time.now.to_i - 900
User.where("last_action > #{time}")
There might be a nicer/safer syntax to put variable arguments in to the where clause, but since you are working with integers (timestamps) this should work and be safe.

Indexing calculated field for search conditions in Thinking Sphinx

I have a products model set up that I am trying to search with Thinking Sphinx. The model has an attribute called status which can be Active, Not active or Active during specified dates.
I would like to be able to restrict my search results to products that are active. I.e. has status of active or has status of active during dates and the current time is between those dates.
I'm a beginner to Rails so I'm looking for suggestions as to how I could implement this. I thought about putting a boolean method in my model that calculates this logic, but I don't think that this is indexable by Sphinx.
I am using MySQL as the database server.
Does anyone have any bright ideas?
You're right, ruby methods on your model are not accesible to sphinx. However you can re-create the method as a sphinx attribute. These can easily be made using SQL fragments like so:
class Person < ActiveRecord::base
...
define_index do
has "status = 'active' and now() > start_date and now() < end_date", :as => :active, :type => :boolean
end
...
end
Using a string to specify a field or attribute like this is the same as specifying a custom column when building an SQL query.
try the #indexes method (haven't tried myself, just noticed in googling around)
http://www.slideshare.net/sazwqa/using-thinking-sphinx-with-rails-presentation
slide 11
http://rdoc.info/rdoc/freelancing-god/thinking-sphinx/blob/04320b610b3a665ca1885cc2e6f29354d029e49a/ThinkingSphinx/Index/Builder.html#indexes-instance_method
Also:
http://www.mail-archive.com/rubyonrails-deployment#googlegroups.com/msg02046.html