Reputation: 21
So currently I have an issue where I am populating a database from xml files through my client's API. Each record has an expiration date in it, with the following format:
<offer_ends_at>2010-10-20T07:59:59-04:00</offer_ends_at>
Now, I need to run a query where I only get records that have an expiration in the future. My current code is:
@deals = Deal.where(["city = :city AND status = 'live' AND DATETIME(date_expires) >= DATETIME(:time)", { :city => @city.id, :time => Time.now }])
And when I'm making the record, I use this:
@deal.date_expires = DateTime.parse((entry/"offer_ends_at").inner_html)
And that is how I get the datetime and format it.
Here's where I'm at:
I just need to make sure that I only show records that have not expired. After two days of scouring the net and no working answers, I decided to ask here. Thanks in advance :)
Upvotes: 1
Views: 1027
Reputation: 361
ActiveRecord is built ontop of the Arel library so we can use that to make a more explicit query.
t = Deal.arel_table
@deals = Deal.where(city: @city.id, t[:time].gt(1.second.ago))
This should get all deals in the future.
Upvotes: 0
Reputation: 21
The answer is that my code wasn't actually bad by the look of things. The issue that cropped up was that I had not properly formatted the input date to be something the could be understood when running the query. Simply changing my query from Time.now to Time.now.parse worked.
Upvotes: 1