How to get records created at the current month?

后端 未结 4 774
小蘑菇
小蘑菇 2021-02-15 18:12

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         


        
4条回答
  •  故里飘歌
    2021-02-15 18:45

      scope :this_month, -> { where(created_at: Time.zone.now.beginning_of_month..Time.zone.now.end_of_month) }
    

    and you can call the scope:

    Model.this_month

提交回复
热议问题