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
You could use an ActiveRecord Association Extension:
#app/models/Candidate.rb
Class Candidate < ActiveRecord::Base
has_many :votes do
def from_this_month
where("created_at > ? AND created_at < ?", Time.now.beginning_of_month, Time.now.end_of_month)
end
end
end
This should allow you to call @candidate.votes.from_this_month
to return the required conditional data