AnthonyGalli.com AnthonyGalli.com - 1 month ago 12
Ruby Question

How to paginate users with more than 0 object of attribute boolean: false?

How to paginate users with more than 0

publish
challenges?

controller



Attempt 1

@users, @alphaParams = User
.select{ |user| user.challenges.publish > 0 }
.alpha_paginate(params[:letter], {:pagination_class => "pagination-centered"}){|user| user.name}


Attempt 2

@users, @alphaParams = User
.where(self.challenges.publish > 0)
.alpha_paginate(params[:letter], {:pagination_class => "pagination-centered"}){|user| user.name}


Attempt 3

@users, @alphaParams = User.joins(:challenges)
.where('challenges.publish > 0')
.distinct
.alpha_paginate(params[:letter], {:pagination_class => "pagination-centered"}){|user| user.name}


model



scope :publish, ->{ where(conceal: false) }

Answer

I would use something like this:

users_with_published_challenges_scope = User
  .select('DISTINCT users.*')
  .joins(:challenges)
  .merge(Challenge.published)

@users, @alphaParams = users_with_published_challenges_scope
  .alpha_paginate(
    params[:letter], 
    :pagination_class => "pagination-centered"
  ) { |user| user.name }
Comments