I have a candidate which has_many votes.
I am trying to get the votes of a candidate that were created in the current month?
@candidate.votes.from_this_m
Correct scope
scope :from_this_month, lambda {where("votes.created_at > ? AND votes.created_at < ?", Time.now.beginning_of_month, Time.now.end_of_month)}
This is because in rails the model names are singular(i.e Vote
) and tables created are pural (e.g. votes
) by convection
EDIT
This can be written simpler with lambda {where(created_at: Time.now.beginning_of_month..(Time.now.end_of_month))}
and we need to use lambda due to the reason given in below comments.
Thanx BroiSatse for reminding :D