Last active
November 28, 2018 17:00
-
-
Save martink-io/49cd757a56f6b1314d77068bcdd7a74f to your computer and use it in GitHub Desktop.
find_all.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
.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
Return all users with Door Supervision or The license the Job requires if CCTV is not selected?