문제

I'm using acts_as_votable for my activities model and I've been able to select only the activities that have votes on them with the following code:

@activities = Activity.select("activities.*, COUNT(votes.id)").joins(:votes).group("activities.id").order("created_at desc").page(params[:page]).per_page(72)

But how do I select activities that only have for example greater than 3 votes and only the ones created within the last month?

Does anyone know how I could accomplish this? Thanks in advance.

도움이 되었습니까?

해결책 2

having was what I was looking for. So for example:

@activities = Activity.joins(:votes).group("activities.id").having("count(votes.id) >= ?", 1).order("created_at desc").where(:created_at => 6.months.ago..Time.zone.now.to_date).page(params[:page]).per_page(60)

다른 팁

In example,

Client.where("orders_count = ?", params[:orders])

or

Activity.where("votes.id > ? and created_at > ?", 3, beginning_of_month*)

Also, "Chapter 2: Conditionals" may be of use to you!

*http://apidock.com/rails/DateAndTime/Calculations/beginning_of_month

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top