Skip to content

Instantly share code, notes, and snippets.

# This is a workaround for the Rails issue described in
# https://github.com/rails/rails/issues/17368.
#
# When assigning `_ids=` on a has_many :through association, Rails immediately
# persists the changes to the join table — even before `save` is called. This
# module defers that persistence to an `after_save` callback, so the IDs are
# only written when the parent record is saved.
#
# If you use this, make sure you understand its code and what it does and what
# it doesn't.
@glv
glv / regexp_syntax.md
Last active November 1, 2024 18:55
Ruby and PostgreSQL Regular Expressions

Ruby and Postgres Regular Expression Syntaxes

Ruby's regular expressions are unusually powerful. Postgres' regular expressions are not as powerful, but they come close; close enough that it's possible to do many pattern-based queries and string transformations entirely in a query.

And sometimes, it's very useful to have a single regular expression that works

@waissbluth
waissbluth / _hreflang.html.haml
Last active July 27, 2022 17:57
Ruby on Rails SEO. Add a <link rel="alternate" hreflang="es" href="http://es.example.com/" /> link to every other locale for the same page. Per https://support.google.com/webmasters/answer/189077?hl=en
-# For each locale except the current one
- (I18n.available_locales - [I18n.locale]).each do |locale|
-# Add a link to the current URL but in `locale` language
%link{rel: 'alternate', hreflang: locale, href: url_for(locale: locale)}