good morning guys, i have a problem of selecting all records which are inactive or active in a database, but if i select all estates and all status it returns all the estates and their different status, active or inactive, but if i select all estates and active status it returns all the estates and display that they are all active, same goes with inactive, but when i select all estates and inactive i want it to display all estates that are inactive same goes with the active.
my code is:
if estate_id == ''
if current_user.is_admin?
filter_estates = "e.id IS NOT NULL"
elsif current_user.has_managing_agent?
estates = current_user.business.approved_estates.collect{|e| e.id }
filter_estates = "e.id IN (#{estates})"
elsif current_user.estate_manager? || current_user.board_member?
filter_estates = "e.id = #{current_user.estate_id}"
end
else
filter_estates = "e.id = #{estate_id}"
end
if status == 'All status'
filters_status = "select e.active"
elsif status == 'Inactive'
filters_status = "select * from estates e where e.active = false limit 1"
else
filters_status = "select * from estates e where e.active = true limit 1"
end
# Query the Database.
if estate_id == ''
@query = Estate.find_by_sql"SELECT (e.name) as estate_name, g.name as governing_body,"+
"(select count(*) from stands s where s.estate_id = e.id AND #{filter_estates}) as total_stands, "+
"(#{filters_status}) as estate_status, "+
"(select count(*) from services sp where sp.estate_id = e.id AND #{filter_estates}) as service_providers,"+
"(select count(*) from approved_vendors av where av.estate_id = e.id AND #{filter_estates})as vendors"+
" FROM estates e LEFT JOIN governing_bodies g on e.governing_body_id = g.id AND #{filter_estates} "
else
@query = Estate.find_by_sql"SELECT (e.name) as estate_name, "+
"(select g.name from governing_bodies g where e.governing_body_id = g.id AND #{filter_estates}) as governing_body ,"+
"(select count(*) from stands s where s.estate_id = e.id AND #{filter_estates}) as total_stands, "+
"(select e.active from estates e where e.active = true and e.id = #{estate_id}) as estate_status,"+
"(select count(*) from services sp where sp.estate_id = e.id AND #{filter_estates}) as service_providers,"+
"(select count(*) from approved_vendors av where av.estate_id = e.id AND #{filter_estates})as vendors"+
" FROM estates e WHERE e.id = #{estate_id} group by e.name"
i am using ruby on rails so this code is in a model.
thanx in advance for your help..