refactor for simplicity and trim array elements
update _common_query to combine foriegn keys