mirror of
https://github.com/jbranchaud/til
synced 2026-01-19 06:58:02 +00:00
Compare commits
2 Commits
2028f6cb09
...
61fc021f52
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
61fc021f52 | ||
|
|
46ad33df7e |
@@ -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).
|
||||||
|
|
||||||
_1577 TILs and counting..._
|
_1579 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)
|
||||||
@@ -905,6 +905,7 @@ See some of the other learning resources I work on:
|
|||||||
- [Two Ways To Compute Factorial](postgres/two-ways-to-compute-factorial.md)
|
- [Two Ways To Compute Factorial](postgres/two-ways-to-compute-factorial.md)
|
||||||
- [Two Ways To Escape A Quote In A String](postgres/two-ways-to-escape-a-quote-in-a-string.md)
|
- [Two Ways To Escape A Quote In A String](postgres/two-ways-to-escape-a-quote-in-a-string.md)
|
||||||
- [Types By Category](postgres/types-by-category.md)
|
- [Types By Category](postgres/types-by-category.md)
|
||||||
|
- [Unable To Infer Data Type In Production](postgres/unable-to-infer-data-type-in-production.md)
|
||||||
- [Union All Rows Including Duplicates](postgres/union-all-rows-including-duplicates.md)
|
- [Union All Rows Including Duplicates](postgres/union-all-rows-including-duplicates.md)
|
||||||
- [Use A psqlrc File For Common Settings](postgres/use-a-psqlrc-file-for-common-settings.md)
|
- [Use A psqlrc File For Common Settings](postgres/use-a-psqlrc-file-for-common-settings.md)
|
||||||
- [Use A Trigger To Mirror Inserts To Another Table](postgres/use-a-trigger-to-mirror-inserts-to-another-table.md)
|
- [Use A Trigger To Mirror Inserts To Another Table](postgres/use-a-trigger-to-mirror-inserts-to-another-table.md)
|
||||||
@@ -1074,6 +1075,7 @@ See some of the other learning resources I work on:
|
|||||||
- [Set DateTime To Include Time Zone In Migrations](rails/set-datetime-to-include-time-zone-in-migrations.md)
|
- [Set DateTime To Include Time Zone In Migrations](rails/set-datetime-to-include-time-zone-in-migrations.md)
|
||||||
- [Set Default As SQL Function In Migration](rails/set-default-as-sql-function-in-migration.md)
|
- [Set Default As SQL Function In Migration](rails/set-default-as-sql-function-in-migration.md)
|
||||||
- [Set default_url_options For Entire Application](rails/set-default-url-options-for-entire-application.md)
|
- [Set default_url_options For Entire Application](rails/set-default-url-options-for-entire-application.md)
|
||||||
|
- [Set Meta Tags In ERB Views](rails/set-meta-tags-in-erb-views.md)
|
||||||
- [Set Schema Search Path](rails/set-schema-search-path.md)
|
- [Set Schema Search Path](rails/set-schema-search-path.md)
|
||||||
- [Set Statement Timeout For All Postgres Connections](rails/set-statement-timeout-for-all-postgres-connections.md)
|
- [Set Statement Timeout For All Postgres Connections](rails/set-statement-timeout-for-all-postgres-connections.md)
|
||||||
- [Set The Default Development Port](rails/set-the-default-development-port.md)
|
- [Set The Default Development Port](rails/set-the-default-development-port.md)
|
||||||
|
|||||||
45
postgres/unable-to-infer-data-type-in-production.md
Normal file
45
postgres/unable-to-infer-data-type-in-production.md
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
# Unable To Infer Data Type In Production
|
||||||
|
|
||||||
|
Inspired by [You Probably Don't Need Query
|
||||||
|
Builders](https://mattrighetti.com/2025/01/20/you-dont-need-sql-builders), I
|
||||||
|
wrote a query in one of my applications that has filter clauses that get
|
||||||
|
short-circuited if the filter value hasn't been included.
|
||||||
|
|
||||||
|
That query looked something like this:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
@tags =
|
||||||
|
Tag.where("? is null or normalized_value ilike ?", normalized_query, "%#{normalized_query}%")
|
||||||
|
.order(:normalized_value)
|
||||||
|
.limit(10)
|
||||||
|
```
|
||||||
|
|
||||||
|
The `normalized_value ilike ?` filtering won't be applied if the
|
||||||
|
`normalized_query` value isn't present (`nil`). This helps me avoid writing
|
||||||
|
messy ternaries or if-else conditional query building madness.
|
||||||
|
|
||||||
|
Unfortunately, when I shipped this query to production, the page started
|
||||||
|
failing and Postgres was reporting this error in the logs.
|
||||||
|
|
||||||
|
```
|
||||||
|
Caused by: PG::IndeterminateDatatype (ERROR: could not determine data type of parameter $1)
|
||||||
|
```
|
||||||
|
|
||||||
|
The query is prepared as a parameterized statement and Postgres appears to be
|
||||||
|
unable to determine the datatype of the first parameter (`$1`) —
|
||||||
|
`normalized_query`.
|
||||||
|
|
||||||
|
I was unable to reproduce the issue in development. It was only occuring in
|
||||||
|
production. Until I can come up with a root cause analysis, I have the
|
||||||
|
following fix that does a casting to `text`. This helps out with the type
|
||||||
|
inference and makes the issue go away.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
@tags =
|
||||||
|
Tag.where("cast(? as text) is null or normalized_value ilike ?", normalized_query, "%#{normalized_query}%")
|
||||||
|
.order(:normalized_value)
|
||||||
|
.limit(10)
|
||||||
|
```
|
||||||
|
|
||||||
|
Interestingly, this person using `pgtyped` [ran into the exact same issue with
|
||||||
|
the same type of query](https://github.com/adelsz/pgtyped/issues/354).
|
||||||
52
rails/set-meta-tags-in-erb-views.md
Normal file
52
rails/set-meta-tags-in-erb-views.md
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
# Set Meta Tags In ERB Views
|
||||||
|
|
||||||
|
There are all kinds of meta tags that we may want to set for the pages that our
|
||||||
|
Rails app serves. A lot of these are for SEO and social sharing. Let's look at
|
||||||
|
how to add `og:description` meta tags to our views.
|
||||||
|
|
||||||
|
I'll start with a helper method in `app/helpers/application_helper.rb`:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
module ApplicationHelper
|
||||||
|
def meta_description(desc)
|
||||||
|
content_for(:description) { desc }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
Then, I'll update my `app/views/layouts/application.html.erb` to consume the
|
||||||
|
description when provided.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<!-- ... -->
|
||||||
|
|
||||||
|
<meta
|
||||||
|
property="og:description"
|
||||||
|
content="<%= content_for?(:description) ? yield(:description) : 'Default description' %>"
|
||||||
|
>
|
||||||
|
|
||||||
|
<!-- ... -->
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<!-- ... -->
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
Now I have a default description for all my views that I can override as needed
|
||||||
|
with the `meta_description` helper.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
# app/views/posts/show.html.erb
|
||||||
|
<%= meta_description @post.body.split("\n").first %>
|
||||||
|
|
||||||
|
<!-- ... -->
|
||||||
|
```
|
||||||
|
|
||||||
|
If I reload the page and inspect the meta tags in `<head>`, I should find the
|
||||||
|
`og:description` tag with the corresponding value.
|
||||||
|
|
||||||
|
This can be extended to apply all the different meta tags (e.g. Open Graph and
|
||||||
|
Twitter) to make links to these pages render well across the internet.
|
||||||
Reference in New Issue
Block a user