Substrack in where clause - ruby-on-rails

I'm writting rake task, but cant get my code to work. Is it possible to write like this?
Model.where('(? - begins_at)/3600 > ?', Time.now, 2).all
WHat am i trying to achieve is to return me list of records that are older then 2 hours

Try with that:
Model.where('begins_at < ?', Time.zone.now - 2.hours).all

Try this:
Model.where(['(? - begins_at) > ?', Time.now, 1]).all
As per your comment you can do like this:
Model.where('begins_at < ?', Time.now - 2.hours).all

Related

Get all records created today in Rails

I want to get all the records from the database which are create_at today. But when I do
Model.where('DATE(created_at) = ?',Date.today)
it returns only the last record that is created today. I have 3 records with created_at
2015-03-03 11:38:31
2015-03-03 11:59:00
2015-03-03 11:33:04
. But it returns only the record with '2015-03-03 11:38:31' this created_at date.
Also referred How to delete all records created today?, Getting all rows created today. I want to know why is this happening?
Also tried
Model.where("DATE(created_at) = ?",Date.today)
and
Tracking::TrackingLogin.where("created_at >= ?", Time.zone.now.beginning_of_day)
Please someone explain what is happening in my code?
Try the following:
Model.where("created_at >= ?", Time.zone.now.beginning_of_day)
Model.where("created_at >= ?", Time.zone.now.beginning_of_day)
Model = your model name
(i.e,. User.where("created_at >= ?", Time.zone.now.beginning_of_day))
Model.where("created_at >= ?", Date.today)

Rails 3.1 - quick rewrite on deprecated query needed

I have this:
Product.find(:all, :conditions => ['release_date >=? AND release_date <=?', #start, #start + #weeks.weeks], :order => "initial_stock DESC")
I understand conditions is now deprecated. This works fine locally but when I upload to heroku the order doesn't work, so probably best I rewite right? Problem is each thing i've tried throws an error. Can anyone help?
Thanks!
This should do it:
Product.where("release_date >= ? AND release_date <= ?", #start, #start + #weeks.weeks).order("initial_stock DESC")
If this is used across the app I generally like to create a scope on the model for it. I haven't tested this code but here's what I would do:
# scope on Product.rb
scope :by_release_date, lambda { |date| where("release_date BETWEEN ? AND ?", date.beginning_of_day, date.end_of_day) }
# query anywhere in app
Product.by_release_date('2012-06-11 00:00:00').order('initial_stock DESC')

WHERE statement in Rails?

Just wondering what the correct way of structuring statements like this is?
#products = Product.where('release_date <= ?', Date.today AND 'physical_status' == 'active')
Thanks!
What you're looking for is this:
#products = Product.where('release_date <= ? AND physical_status = ?', Date.today, 'active')
You can take a look at this guide: http://guides.rubyonrails.org/active_record_querying.html

rails find created_on withing last 5 minutes

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")

How do I use .find for the last five days in ruby on rails?

Have a model called contact_email.date_sent
I want to be able to run a report which displays all those where the date_sent range is between date.today and date.today 5 days ago.
I assume I use something like
#send_emails = Contact_Email.find(:conditions=> ???)
But not clear what exactly is the best way. Thanks!
Try this:
ContactEmail.all(:conditions => ["date_sent >= ?", 5.days.ago.to_date])
This approach is faster than using BETWEEN clause( assuming date_sent is indexed)
Caveat:
Value of date_sent column should be less than current date.
Edit 1
To add an index in migration:
add_index :contact_emails, :date_sent
ContactEmail.find(:conditions => ['date_sent BETWEEN ? AND ?', Date.today, 5.day.ago.to_date])
If it's something you will use regularly, why not put a named_scope in the model:
named_scope :recent, lambda { |*args| {:conditions => ["date_sent > ?", (args.first || 5.days.ago)]} }
which will let you write:
ContactEmail.recent
for the last 5 days worth, or use the arg to specify your own time frame e.g. the last two weeks:
ContactEmail.recent(2.weeks.ago)

Resources