Reputation: 695
I have a database where I am trying to find data within a certain range and meeting certain criteria. I am using the following command:
GameSlot.where("s = ? and st >= :start_date and et <= :end_date", 5, {start_date: '2016-01-01', end_date: '2017-01-01'})
When I execute, I get back:
ArgumentError: wrong number of arguments (3 for 0..1)
I am a novice to Ruby on Rails so any help is appreciated.
EDIT: The datatypes for "s" is integer, "st" and "et" are time, but I am only interested in the date.
Upvotes: 0
Views: 143
Reputation: 20263
As an alternative approach, you could consider class methods (or scopes, if you like that sort of thing). Something like:
class GameSlot < ActiveRecord::Base
class << self
def s(val) where(s: val) end
def starting_on_or_after(date) where('st >= ?', date) end
def ending_on_or_before(date) where('et <= ?', date) end
end
end
Then you could do something like:
GameSlot.s(5).starting_on_or_after('2016-01-01').ending_on_or_before('2017-01-01')
That way, you leave the query logic inside the GameSlot
model which seems a little better encapsulated (to me).
Something to chew on.
Upvotes: 1
Reputation: 10507
I guess you are looking for something like this:
GameSlot.where("s = ? and st >= ? and et <= ?", 5, '2016-01-01', '2017-01-01')
Upvotes: 2
Reputation: 230386
Should be more like this, I think:
GameSlot.where(["s = :s and st >= :start_date and et <= :end_date",
s: 5,
start_date: '2016-01-01',
end_date: '2017-01-01'])
Note that where
here only has one argument, an array.
Documentation on QueryMethods#where.
Upvotes: 3