I’ve a model named GuestOrder:
class GuestOrder < ActiveRecord::Base
end
# == Schema Information
#
# Table name: guest_orders
#
# id :integer not null, primary key
# notes :string(255)
# adults :integer
# children :integer
# created :datetime
# placed :datetime
# billed :datetime
# user_id :integer
# guest_table_id :integer
# take_away_id :integer
# created_at :datetime
# updated_at :datetime
And a controller
class ApiController < ApplicationController
def getOrdersByDate
@guest_orders = GuestOrder.where(:created_at => (params[:created]))
render json: @guest_orders
end
end
In routes.rb
match '/api/getOrdersByDate'
When I try to get this in url
http://localhost:3000/api/getOrdersByDate?:created=2012-4-5
I got all the records not restricted by date
In server I got select * from guest_orders
Started GET "/api/getOrdersByDate?:created=2012-4-5" for 127.0.0.1 at 2012-04-06 13:19:59 +0530
Processing by ApiController#getOrdersByDate as HTML
Parameters: {":created"=>"2012-4-5"}
GuestOrder Load (26.3ms) SELECT "guest_orders".* FROM "guest_orders"
Completed 200 OK in 47ms (Views: 1.5ms | ActiveRecord: 26.3ms)
Started GET "/api/getOrdersByDate?:created=2012-4-5" for 127.0.0.1 at 2012-04-06 13:21:21 +0530
Processing by ApiController#getOrdersByDate as HTML
Parameters: {":created"=>"2012-4-5"}
GuestOrder Load (1.7ms) SELECT "guest_orders".* FROM "guest_orders"
Completed 200 OK in 47ms (Views: 1.4ms | ActiveRecord: 1.7ms)
Records in my DB have records in date 2012-03-22, 2012-04-05, 2012-04-06
How can I query in getOrdersByDate to get records of a particular date.
You don’t need the colon in your request,
HashWithIndifferentAccesswiill do the sym lookup for you.But the problem looks interesting, because passing a
nilto a where conditions should cause the following query:And in your case the whole SQL WHERE condition is missing.
Update #1
Seems like reloading needed.
To fetch the records for a date you could use the following method:
Update #2
If you have a start and an end date: