{"id":15353765,"url":"https://github.com/westonganger/active_record_simple_execute","last_synced_at":"2025-02-27T01:31:09.215Z","repository":{"id":62552890,"uuid":"342497282","full_name":"westonganger/active_record_simple_execute","owner":"westonganger","description":"Sanitize and Execute your raw SQL queries in ActiveRecord and Rails with a much more intuitive and shortened syntax","archived":true,"fork":false,"pushed_at":"2024-12-03T05:49:49.000Z","size":56,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-19T15:51:48.557Z","etag":null,"topics":["activerecord","rails","ruby","sql"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/westonganger.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-26T07:29:32.000Z","updated_at":"2024-12-03T05:50:00.000Z","dependencies_parsed_at":"2023-01-30T17:01:37.919Z","dependency_job_id":"35a942d5-4904-4d8a-a46f-9057af46822f","html_url":"https://github.com/westonganger/active_record_simple_execute","commit_stats":{"total_commits":13,"total_committers":1,"mean_commits":13.0,"dds":0.0,"last_synced_commit":"f1f5d1bd5402dc060d419414da034001859eb9b2"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Factive_record_simple_execute","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Factive_record_simple_execute/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Factive_record_simple_execute/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Factive_record_simple_execute/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/westonganger","download_url":"https://codeload.github.com/westonganger/active_record_simple_execute/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240961273,"owners_count":19885298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","rails","ruby","sql"],"created_at":"2024-10-01T12:15:07.957Z","updated_at":"2025-02-27T01:31:09.177Z","avatar_url":"https://github.com/westonganger.png","language":"Ruby","readme":"# ActiveRecord Simple Execute\n\n\u003ca href=\"https://badge.fury.io/rb/active_record_simple_execute\" target=\"_blank\"\u003e\u003cimg height=\"21\" style='border:0px;height:21px;' border='0' src=\"https://badge.fury.io/rb/active_record_simple_execute.svg\" alt=\"Gem Version\"\u003e\u003c/a\u003e\n\u003ca href='https://github.com/westonganger/active_record_simple_execute/actions' target='_blank'\u003e\u003cimg src=\"https://github.com/westonganger/active_record_simple_execute/actions/workflows/test.yml/badge.svg?branch=master\" style=\"max-width:100%;\" height='21' style='border:0px;height:21px;' border='0' alt=\"CI Status\"\u003e\u003c/a\u003e\n\u003ca href='https://rubygems.org/gems/active_record_simple_execute' target='_blank'\u003e\u003cimg height='21' style='border:0px;height:21px;' src='https://img.shields.io/gem/dt/active_record_simple_execute?color=brightgreen\u0026label=Rubygems%20Downloads' border='0' alt='RubyGems Downloads' /\u003e\u003c/a\u003e\n\n\u003e [!WARNING] \n\u003e GEM DEPRECATED: I now recommend to just use `connection.select_all(Arel.sql(sql, foo: \"dead\", bar: \"beef\")).to_a`\n\nSanitize and Execute your raw SQL queries in ActiveRecord and Rails with a much more intuitive and shortened syntax.\n\nThis gem is in response to a lack of proper documentation of best practices within Rails - I've [created a documentation PR](https://github.com/rails/rails/pull/53719) to resolve this but until this this is merged this gem seems necessary for the eco-system.\n\n# Installation\n\n```ruby\ngem \"active_record_simple_execute\"\n```\n\n## Comparison with Plain ActiveRecord\n\nAs seen here using `simple_execute` is much easier to remember than all the hoops plain ActiveRecord makes you jump through.\n\n### Using `simple_execute`\n```ruby\nsql_str = \u003c\u003c~SQL.squish\n  SELECT * FROM orders\n  FROM orders\n  WHERE orders.company_id = :company_id AND orders.updated_by_user_id = :user_id\nSQL\n\nrecords = ActiveRecord::Base.connection.simple_execute(sql_str, company_id: @company.id, user_id: @user.id)\n# OR use the convenience method excluding the connection portion\n# ActiveRecord::Base.simple_execute(...)\n```\n\n### Using original ActiveRecord `select_all` or `exec_query` method\n```ruby\nsql_str = \u003c\u003c~SQL.squish\n  SELECT *\n  FROM orders\n  WHERE orders.company_id = :company_id AND orders.updated_by_user_id = :user_id\nSQL\n\n### FOR READ OPERATIONS\nsanitized_sql = Arel.sql(sql_str, company_id: @company.id, user_id: @user.id)\nresult = ActiveRecord::Base.connection.select_all(sanitized_sql)\n\n### OR FOR WRITE OPERATIONS (you probably shouldnt be doing this anyways)\n### (while exec_query is capable of read \u0026 write operations, recommended only for write operations as it affects the query cache)\nsanitized_sql = ActiveRecord::Base.sanitize_sql_array([sql_str, {company_id: @company.id, user_id: @user.id}]) # Must use sanitize_sql_array, since Arel.sql is not yet compatible with `exec_query`\nresult = ActiveRecord::Base.connection.exec_query(sanitized_sql) # recommended only for write operations as it affects the query cache\n\nrecords = result.to_a # convert the ActiveRecord::Result object into an array of hashes\n\nreturn records\n```\n\n### Using original ActiveRecord `execute` method\n\nIt should be noted that it is recommended to avoid all usage of `execute` and to instead use `select_all` or `exec_query` which returns generic ActiveRecord::Result objects\n\n```ruby\nsql_str = \u003c\u003c~SQL.squish\n  SELECT *\n  FROM orders\n  WHERE orders.company_id = :company_id AND orders.updated_by_user_id = :user_id\nSQL\n\n# Must use sanitize_sql_array, since Arel.sql is not yet compatible with `execute`\nsanitized_sql = ActiveRecord::Base.sanitize_sql_array([sql_str, {company_id: @company.id, user_id: @user.id}])\n\nresult = ActiveRecord::Base.connection.execute(sanitized_sql)\n\nif defined?(PG::Result) \u0026\u0026 result.is_a?(PG::Result)\n  records = result.to_a\n\n  result.clear # to prevent memory leak\n\nelsif defined?(Mysql2::Result) \u0026\u0026 result.is_a?(Mysql2::Result)\n  records = []\n\n  result.each do |row|\n    h = {}\n\n    result.fields.each_with_index do |field,i|\n      h[field] = row[i]\n    end\n\n    records \u003c\u003c h\n  end\n\nelse\n  records = result\nend\n\nreturn records\n```\n\n# Testing\n\n```\nbundle exec rake test\n```\n\nWe can locally test different versions of Rails using `ENV['RAILS_VERSION']`\n\n```\nexport RAILS_VERSION=7.0\nbundle install\nbundle exec rake test\n```\n\nFor quicker feedback during gem development or debugging feel free to use the provided `rake console` task. It is defined within the [`Rakefile`](./Rakefile).\n\n# Credits\n\nCreated \u0026 Maintained by [Weston Ganger](https://westonganger.com) - [@westonganger](https://github.com/westonganger)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwestonganger%2Factive_record_simple_execute","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwestonganger%2Factive_record_simple_execute","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwestonganger%2Factive_record_simple_execute/lists"}