I have a birth_date variable in the Date format. I want to compare it to Date.today as shown below. The problem is it is coming back false because it wants to compare the year as well. It is a birthday so I don't care about the year just trying to see if birth_date (month and day) is equal to Date.today.day.month.
Any ideas?
bdays = Soldier.find(:all, :conditions => ["birth_date LIKE ?", Date.today] )
You will need to break up your date, because you want to ignore the year. You will need to use some of the functions given by your SQL provider (the example below uses MySQL):
bdays = Soldier.find(:all, :conditions => ["DAY(birth_date) = ? AND MONTH(birth_date) = ?", Date.today.day, Date.today.month])
if you're using SQLite (rails' default database), it will be a bit more complicated because they don't have a real date type:
bdays = Soldier.find(:all, :conditions => ["STRFTIME('%d', birth_date) = ? AND STRFTIME('%m', birth_date) = ?", Date.today.day, Date.today.month])
Figured I would add postgres:
Soldier.where("EXTRACT(DAY FROM birth_date) = ? AND EXTRACT(MONTH FROM birth_date) = ?", Date.today.day, Date.today.month)
Related
I'm trying to find all the records that have a specific channel_id, and were made less than 1 day ago. The query finds the records with the given channel_id but does not respect the date restriction. Instead, it returns records created at any date.
#discussions = Discussion.where('channel_id = ? and created_at > ?', current_user.subscription.pluck(:channel_id), 1.days.ago )
I expected to only get the Discussions that had relevant subscriptions channel_id, and were made less than 1 day ago. But instead, this query ignores the created at restriction but returns the Discussions that had the corresponding subscriptions channel ID.
#discussions = Discussion.where("channel_id IN (?) AND DATE(created_at) >= ?", current_user.subscription.pluck(:channel_id), (Date.today - 1.day))
OR
#discussions = Discussion.where("channel_id IN (?) AND created_at >= ?", current_user.subscription.pluck(:channel_id), (Date.today - 1.day).end_of_day)
Or
Discussion.where("DATE(created_at) >= ?", (Date.today - 1.day))
.where("channel_id IN (?)", current_user.subscription.pluck(:channel_id))
#discussions = Discussion.where("channel_id = :channel AND created_at > :date", channel: current_user.subscription.pluck(:channel_id), date: Date.yesterday )
I am using gem called impressionist to log page views on show action.
Everythink works just great.I can get number of all pageviews with:
#advertisement.impression_count
But now I want to be able filter pageviews per today, yesterday and this month.
So far I came up with this solution.
#today = Impression.where( :conditions => { :created_at => Date.today...Date.today+1 }, :impresionable_id =>#advertisement.id)
There is no errors.
Then In view:
<%= "#{#today} views so far!" %>
gives me #<Impression::ActiveRecord_Relation:0x000000068d46f8>
then I tried to add like : <%= "#{#today.impression_count} views so far!" %> gives me this :
undefined method `impression_count'
then I tried just :<%= "#{#today.count} views so far!" %> and still error:
Mysql2::Error: Unknown column 'conditions.created_at' in 'where clause': SELECT COUNT(*) FROM `impressions` WHERE (`conditions`.`created_at` >= '2014-12-18' AND `conditions`.`created_at` < '2014-12-19') AND `impressions`.`impresionable_id` = 127
Any ideas ?
Thanks in advance!
#today = Impression.where( :conditions => { :created_at => Date.today...Date.today+1 }, :impresionable_id =>#advertisement.id)
returns a #<Impression::ActiveRecord_Relation:0x000000068d46f8>.
Try this:
#today = Impression.where(created_at: Date.today...Date.today+1, impresionable_id: #advertisement.id).count
Add scopes in impression.rb
scope :today, -> {where("created_at >= ? AND created_at < ?", Time.now.beginning_of_day, Time.now.end_of_day)}
scope :yesterday, -> {where("created_at >= ? AND created_at < ?", 1.day.ago.beginning_of_day, 1.day.ago.end_of_day)}
scope :this_month, -> {where("created_at >= ? AND created_at < ?", Time.now.beginning_of_month, Time.now.end_of_month)}
in Controller:
#today = Impression.today.where(impresionable_id: #advertisement.id)
#yesterday = Impression.yesterday.where(impresionable_id: #advertisement.id)
#this_month = Impression.this_month.where(impresionable_id: #advertisement.id)
And you can use these scopes anywhere you need to filter Impressions by date today, yesterday or this month. It's better compared to writing the where clause everywhere.
There's no need for the conditions hash.
today = Date.today
range = today..today.next_day
#imp = Impression.where(created_at: range, impressionable_id: #advertisement.id)
And if an #advertisement can have impressions, then the following would be better:
#imp = #advertisement.impressions.where(created_at: range)
Then to get the count, you must:
#today = #imp.count
Also, just FYI, you might need to use DateTime.now instead of Date.today because you're comparing with a datetime field i.e. created_at.
It was easier than I though
In advertisement.rb
has_many :impressions, :as=>:impressionable
def view_count_yesterday
impressions.where("created_at >= ? AND created_at < ?", 1.day.ago.beginning_of_day, 1.day.ago.end_of_day).size
end
def view_count_today
impressions.where("created_at >= ? AND created_at < ?", Time.now.beginning_of_day, Time.now.end_of_day).size
# impressionist_count(:start_date => 1.day.ago)
end
With Ruby on Rails console, is it possible to query the database for all records created on a certain day?
something like
date = "january 5 2013"
users = User.find(:all, :conditions => {:created_at => date})
You can do it like this:
date = Date.parse('january 5 2013')
users = User.where(created_at: date.midnight..date.end_of_day)
Try this,
User.where("Date(updated_at) = ?", "2018-02-9")
You can also do it like
User.where("created_at::date = ?", "january 5 2013".to_date)
Yes, It is possible like:
date = Date.parse("january 5 2013")
users = User.where(created_at: date)
but created_at is type of date-time like 2014-01-28 08:35:00.9608
and I think All user have different created_at
So you may used like this
User.where("created_at = ?", "2014-01-23 16:19:48.199086")
If you see the follow link Don't use BETWEEN (especially with timestamps)
You will note that you can have problems.
Instead you can use
users = User.where('created_at >= ? and created_at <=?', date.midnight, date.end_of_day)
"SELECT \"users\".* FROM \"users\" WHERE (created_at >= '2018-05-17 07:00:00' and created_at <='2018-05-18 06:59:59.999999')"
I have a model (Entries) with five years worth of records (one record per day). I need a method that, when passed a date object such as 2011-12-25 00:00:00, will show me ALL the records that have happened on 12/25 (querying against the :created_at column), regardless of the year or time that's passed.
RoR 3.0.9 / Ruby 1.9.2p290
You can use the MONTH and DAY values of mysql. Maybe something like:
Model.where("MONTH(created_at) = ? and DAY(created_at) = ?", somedate.month, somedate.day)
A general solution that should work with most SQL databases (include MySQL and PostgreSQL):
Entry.where('extract(month from created_at) = ? AND extract(day from created_at) = ?', d.month, d.day)
SQLite doesn't understand extract though so you'd have to use:
Entry.where("strftime('%m/%d', created_at) = ?", d.strftime('%m/%d'))
Assuming that you are using mysql:
User.where(["DAY(created_at) = ? AND MONTH(created_at) = ?", date.day, date.month])
Assume RDBMS is MySQL and you have form with combobox to select month and/or date_of_months, may be you could make a named_scope, for example :
scope :by_date_or_month, lambda { |date, month| {:conditions => ["DAYOFMONTH(created_at) = ? or MONTH(created_at) = ?", date, month]}}
Test from IRB :
Model.by_date_or_month(31,8)
Model.by_date_or_month(nil,8)
I have the following code:
#gameRequests = GameRequest.find(:first, :conditions => ["created_on >= ?", created_on])
I'd like to find the oldest record within the last 5 minutes. How can I subtract the 5 minutes from created_on to do this? I cant seem to find any examples.
Thanks
The code you want is:
#gameRequest = GameRequest.first(:conditions => ["created_on >= ?", DateTime.now - 5.minutes])
BTW, are you sure the field is named created_on and not created_at? Also, .first gets only the first record, not all of them. For that, you need .all
gameRequests = GameRequest.find(:first, :conditions => ["created_on >= ?", created_on - 5.minute])
Try this:
#gameRequests = GameRequest.find(:first, :conditions => ["created_at >= ?", Time.now - 5.minutes], :order => "created_at ASC")