Track daily users signin - ruby-on-rails

So I want to output users that signed in 2 hours ago with the hope I will call it on the front end, and I have a database attribute (created_at)that keeps track of the time you sign in.
What I did was to define a scope to handle that which got nothing from. Here is my code below:
class SignupHistory < ActiveRecord::Base
scope :one_hour_ago, -> { where 'created_at > ?', Time.now - 2.hours.ago }
end
How do I go about showing users that logged in 2 hours ago?

Am I correct to understand that you are trying to find all the records that were created within the last two hours? Then, you can just test against 2.hours.ago without any subtraction operator:
scope :two_hours_ago, -> { where 'created_at > ?', 2.hours.ago }

I think your problem is that you're calling .ago.
You should be able to accomplish what you need by calling:
scope :one_hour_ago, -> { where 'created_at > ?', Time.zone.now - 2.hours }

Related

Rails: Ordering objects created in 3 days

I want to display the hottest posts in 3 days, so this is what I do
#posts = Post.created_in_days(3).order_by_likes.first(4)
For queries:
scope :order_by_likes, -> { order('likes IS NULL, likes DESC') }
scope :created_in_days, ->(number) {where('created_at >= ?', Time.zone.now - number.days)}
This works fine. But the problem is, sometimes there is not enough posts generate in 3 days. Maybe just 3, or even no post at all.
How can I handle this so when #posts created in 3 days is not enough, it would extend the time, till it get enough #posts? (In this case, 4 post needed).
scope :created_in_days, ->(number) do
filtered = where('created_at >= ?', Time.zone.now - number.days)
if filtered.length < 4
order(:created_at => :desc).limit(4) # takes last 4 created
else
filtered
end
end

Counting User registration for a day, week, month

I want to count the users that registered on the website on specific periods.
For example:
Today: 5
Yesterday: 7
Over last week: 28
Over last month: 101
I used this stackoveflow question that is somewhat relevant to what I want to do. But when I try to apply it it has several problems in terms of logic for what I try to do.
So what I figured out is that I should use something like:
#lastfivedays = User.where(
'created_at >= :five_days_ago',
:five_days_ago => Time.now - 5.days,
)
But where am I placing this and how do I use it in the view?
Yes I am lost on how I do something like this in Rails as I am new to this. Any guidance, tip will be extremely helpful.
Your query should go in the controller and you can then access the instance variable in the view.
Controller:
#lastfivedays = User.where(
'created_at >= :five_days_ago',
:five_days_ago => 5.days.ago,
).count
View:
Number of users who registered in last 5 days: <%= #lastfivedays %>
I would recommend using ActiveRecord Scopes
You could have something like:
class User < ActiveRecord::Base
...
scope :joined_within_five_days, -> { where('created_at >= :five_days_ago',
:five_days_ago => Time.now - 5.days,) }
...
end
Your controller could then use the approriate one. For example
#users = User.joined_within_five_days

Rails - How to check what the record count of a model 24 hours ago

I want to display the rate of change in the total number of records for a certain model. So I need to know, at any given time, how many there were 24 hours ago, and how many there are now.
It is very likely that a record will be deleted by users, so I can't just use the value of existing records that are older than 24 hours.
Is there a simple way to do this?
Thanks
class Item < ActiveRecord::Base
scope :since, lambda {|time| where("created_at > ?", time) }
scope :during_last, lambda {|time| where("created_at > ?", (Time.now - time)) }
end
Item.since(Time.now - 24.hours).count
Item.since(24.hours.ago).count
Item.during_last(24.hours).count
Well, you could do a count against the column created_at.
def count_rate(model, time=DateTime.now)
current_count = model.count(:conditions => ["created_at < ?", time])
last_24_hours_count = model.count(:conditions => ["created_at < ?", time-24.hours])
current_count - last_24_hours_count
end
This method count the record at a given compared with the last 24 hours.
Well, in case the user has deleted those records back, you would should create another table Rate(date, hour, count) and record every hour by using observer (only after_create).
def after_create
rate = Rate.find_or_initialize_by(Date.today.to_s, Time.now.hour)
rate.increment(:count)
rate.save!
end
In this observer, if it finds the existing record by date and hour, it simply increment the count column. You add the default value to zero on count column as well. Later on, you can query the data from that table by date and hour.

How can I show closest date-to-happen posts?

I am building a site that displays a weekly resume of what will be going on.
My problem is: an admin can place a post two months from now, how can I restrict those posts from showing up until the week it would actually happen?
Use a scope or two:
class Post < ActiveRecord::Base
scope :future, lambda { where('ends_at > ?', Time.zone.now') }
scope :up_to, lambda { |time| where('starts_at < ?', time) }
end
#coming_up_posts = Post.future.up_to(7.days.from_now)

Get number of rows associated model has with complicated join

I'm new to rails and am unsure of the best way of doing this. I have a User which "has many" Events. I want to be able to go user.active? in a view which will display whether the user has one or more associated Events created in the last 5 minutes.
I know the syntax isn't correct but this is what I've got so far:
def active?
find(:conditions => {:events => ["created_at >= ?", DateTime.now - 5.minute]}).count > 0
end
You can just do a find on the events collection:
def active?
events.where('created_at >= ?', 5.minutes.ago).count > 0
end

Resources