Rails 4.1 fixes

Just restructured the active record queries to work with 4.1. Submitting the pull request to test against older versions for breaking changes.