Skip to content

Instantly share code, notes, and snippets.

@martink-io
Last active November 28, 2018 17:00
Show Gist options
  • Save martink-io/49cd757a56f6b1314d77068bcdd7a74f to your computer and use it in GitHub Desktop.
Save martink-io/49cd757a56f6b1314d77068bcdd7a74f to your computer and use it in GitHub Desktop.
find_all.rb
def licences_query(users)
return users unless params[:licence_ids]
users_scope = users
licence_ids = params[:licence_ids]
if params[:licence_ids].is_a? String
licence_ids = params[:licence_ids].split(',').map{|chr| chr.to_i}
end
# 4 - Door Supervision
# 2 - CCTV
# Return all users with Door Supervision if there is no CCTV selected
# Otherwise, require exact match (else)
if licence_ids.include?(4) && !licence_ids.include?(2)
users_scope = users_scope.joins("INNER JOIN guard_licences as #{rel} ON users.id=#{rel}.user_id")
.where("#{rel}.licence_id=4")
.where("#{rel}.status=?", 'active')
else
licence_ids.each_with_index do |licence_id, index|
rel = "gl#{index}"
users_scope = users_scope.joins("INNER JOIN guard_licences as #{rel} ON users.id=#{rel}.user_id")
.where("#{rel}.licence_id=?", licence_id)
.where("#{rel}.status=?", 'active')
end
end
users_scope
end
@zainfur
Copy link

zainfur commented Nov 28, 2018

Return all users with Door Supervision or The license the Job requires if CCTV is not selected?

@zainfur
Copy link

zainfur commented Nov 28, 2018

.where("#{rel}.licence_id=4" || licence_ids /// will this work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment