-
Notifications
You must be signed in to change notification settings - Fork 22.2k
Add documentation for raw SQL reads and writes capabilities with ActiveRecord #53719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add documentation for raw SQL reads and writes capabilities with ActiveRecord #53719
Conversation
133025e to
c2df36b
Compare
|
|
||
| ```irb | ||
| irb> Customer.find_by_sql("SELECT * FROM customers INNER JOIN orders ON customers.id = orders.customer_id ORDER BY customers.created_at desc") | ||
| irb> Customer.find_by_sql("SELECT * FROM customers INNER JOIN orders ON customers.id = orders.customer_id WHERE orders.company_id = ? ORDER BY customers.created_at desc", [company.id]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now shows how to use the binds argument on find_by_sql
It might be more desirable to steer users away from using this awkward binds argument and instead use Arel.sql
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the example for find_by_sql to use Arel.sql
There are some small differences with where and find_by_sql binds arguments in that for find_by_sql specifically you must wrap your arguments in an explicit array or hash
43ecc07 to
49668cb
Compare
dd2e97b to
29ed0a4
Compare
29ed0a4 to
0ed23a8
Compare
|
|
||
| [`find_or_initialize_by`]: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-find_or_initialize_by | ||
|
|
||
| Finding by SQL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im tempted to say that we should not have a section for find_by_sql in this document at all.
Its absolutely bizarre when people start selecting columns outside of the model you are working with, which is what commonly happens here.
In place of I would suggest a section in select_all where it recommends that any usage of find_by_sql might be better served by the select_all method instead.
Motivation / Background
This Pull Request has been created because of a discussion we had in #53695
The reasoning for these new sections is:
select_alloverexec_queryoverexecutebindsargument of these methods. One would assume they support positional or named binds the same aswhereor any other method, however the reality is they do not. It only supports positional binds and they must be wrapped in an explicit array.nilas the 2nd argument to all these methods prior tobindswhich makes it hard to understand how to properly use themArel.sqlrather than attempt use thebindsargument given the aboveBelow I show the current (poor) experience with binds on
select_all(and other methods)Detail
This Pull Request adds a section to the documentation on the "Active Record Query Interface" page
The new sections are
Additional information
After this documentation is merged. I would suggest that it might be wise to deprecate the
bindsargument from all these methods and instead recommend to useArel.sqlas a replacement. Suggested by @matthewdChecklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]