I’m working with a User model that includes booleans for 6 days
[sun20, mon21, tue22, wed23, thur24, fri25]
with each user having option to confirm which of the 6 days they are participating in.
I’m trying to define a simple helper method:
def day_confirmed(day)
User.where(day: true).count
end
where I could pass in a day and find out how many users are confirmed for that day, like so:
day_confirmed('mon21')
My problem is that when I use day in the where(), rails assumes that I’m searching for a column named day instead of outputting the value that I’m passing in to my method.
Surely I’m forgetting something, right?
This syntax:
Is equivalent to:
That’s because using
:in a hash instead of=>, e.g.{ foo: bar }, is the same as using a symbol for the key, e.g.{ :foo => bar }. Perhaps this will help:So, if you want to use the value of the variable
dayrather than the symbol:dayas the key, try this instead: