mirror of
https://github.com/jbranchaud/til
synced 2026-01-20 15:38:02 +00:00
Compare commits
3 Commits
5fd246c8d8
...
9f6a83d5be
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9f6a83d5be | ||
|
|
0251157dc4 | ||
|
|
295fe153ad |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1582 TILs and counting..._
|
_1583 TILs and counting..._
|
||||||
|
|
||||||
See some of the other learning resources I work on:
|
See some of the other learning resources I work on:
|
||||||
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
|
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
|
||||||
@@ -993,6 +993,7 @@ See some of the other learning resources I work on:
|
|||||||
- [Define The Root Path For The App](rails/define-the-root-path-for-the-app.md)
|
- [Define The Root Path For The App](rails/define-the-root-path-for-the-app.md)
|
||||||
- [Delete Paranoid Records](rails/delete-paranoid-records.md)
|
- [Delete Paranoid Records](rails/delete-paranoid-records.md)
|
||||||
- [Demodulize A Class Name](rails/demodulize-a-class-name.md)
|
- [Demodulize A Class Name](rails/demodulize-a-class-name.md)
|
||||||
|
- [Determine The Configured Primary Key Type](rails/determine-the-configured-primary-key-type.md)
|
||||||
- [Different Ways To Add A Foreign Key Reference](rails/different-ways-to-add-a-foreign-key-reference.md)
|
- [Different Ways To Add A Foreign Key Reference](rails/different-ways-to-add-a-foreign-key-reference.md)
|
||||||
- [Disambiguate Where In A Joined Relation](rails/disambiguate-where-in-a-joined-relation.md)
|
- [Disambiguate Where In A Joined Relation](rails/disambiguate-where-in-a-joined-relation.md)
|
||||||
- [Empty find_by Returns First Record](rails/empty-find-by-returns-first-record.md)
|
- [Empty find_by Returns First Record](rails/empty-find-by-returns-first-record.md)
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ an array-like object with all of the arguments to the function. Even if not
|
|||||||
all of the arguments are referenced in the function signature, they can
|
all of the arguments are referenced in the function signature, they can
|
||||||
still be accessed via the `arguments` object.
|
still be accessed via the `arguments` object.
|
||||||
|
|
||||||
|
> For ES6+ compatibility, the `spread` operator used via [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) is preferred over the `arugments` object when accessing an abritrary number of function arguments.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
function argTest(one) {
|
function argTest(one) {
|
||||||
console.log(one);
|
console.log(one);
|
||||||
|
|||||||
35
rails/determine-the-configured-primary-key-type.md
Normal file
35
rails/determine-the-configured-primary-key-type.md
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# Determine The Configured Primary Key Type
|
||||||
|
|
||||||
|
I noticed an interesting helper function in the database migration generated by
|
||||||
|
`bin/rails active_storage:install`.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class CreateActiveStorageTables < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
# Use Active Record's configured type for primary and foreign keys
|
||||||
|
primary_key_type, foreign_key_type = primary_and_foreign_key_types
|
||||||
|
|
||||||
|
# ...
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def primary_and_foreign_key_types
|
||||||
|
config = Rails.configuration.generators
|
||||||
|
setting = config.options[config.orm][:primary_key_type]
|
||||||
|
primary_key_type = setting || :primary_key
|
||||||
|
foreign_key_type = setting || :bigint
|
||||||
|
[ primary_key_type, foreign_key_type ]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
The `primary_and_foreign_key_types` method looks in the generators config for
|
||||||
|
the ORM (`:active_record`) to determine the configured `:primary_key_type`. By
|
||||||
|
default this will return `nil`. This method then uses `:primary_key` as a
|
||||||
|
fallback value which will be `bigint`. That's why the `foreign_key_type` falls
|
||||||
|
back to `:bigint`.
|
||||||
|
|
||||||
|
If desired, this can be manually configured in `config/application.rb` like
|
||||||
|
shown in the [ActiveRecord Migrations
|
||||||
|
docs](https://guides.rubyonrails.org/active_record_migrations.html#enabling-uuids-in-rails).
|
||||||
Reference in New Issue
Block a user