<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Gaëtan Masson</title>
    <description>The latest articles on Forem by Gaëtan Masson (@gaetanm).</description>
    <link>https://forem.com/gaetanm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F16554%2F027cbe72-64e7-4810-bf8a-d0a03477d480.jpeg</url>
      <title>Forem: Gaëtan Masson</title>
      <link>https://forem.com/gaetanm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gaetanm"/>
    <language>en</language>
    <item>
      <title>Soft Deletion, Nested Resource and Active Admin</title>
      <dc:creator>Gaëtan Masson</dc:creator>
      <pubDate>Thu, 05 Aug 2021 08:15:50 +0000</pubDate>
      <link>https://forem.com/gaetanm/soft-deletion-nested-resource-and-active-admin-2nd2</link>
      <guid>https://forem.com/gaetanm/soft-deletion-nested-resource-and-active-admin-2nd2</guid>
      <description>&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;models/order_payments.rb&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;deletable_from_admin&lt;/span&gt;
  &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;validated?&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;deleted_at&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;blank?&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;destroy&lt;/span&gt;
  &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;context&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="ss"&gt;:update_from_admin_space&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;update!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;deleted_at: &lt;/span&gt;&lt;span class="no"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;admin/order.rb&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;controller&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update&lt;/span&gt;
    &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ss"&gt;:update_from_admin_space&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inputs&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:order_payments&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;new_record: &lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;payment_accepted?&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;allow_destroy: :deletable_from_admin&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;order_payment&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;order_payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deleted_at&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;blank?&lt;/span&gt;
      &lt;span class="n"&gt;order_payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt; &lt;span class="ss"&gt;:expected_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Details
&lt;/h2&gt;

&lt;p&gt;In my company, we are currently implementing a soft deletion strategy on some tables. The task seems easy: add a new field  &lt;code&gt;deleted_at&lt;/code&gt; and fill it with an &lt;code&gt;update&lt;/code&gt; whenever &lt;code&gt;destroy&lt;/code&gt; has normally to be called.&lt;/p&gt;

&lt;p&gt;However, when you are using a gem doing a lot of magic (hello Active Admin) and want to do something specific, things get more tricky.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/WPRJfpqxa9RF6/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/WPRJfpqxa9RF6/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So first I had to show the data. It was quite easy, just had to call my custom &lt;code&gt;has_many&lt;/code&gt; instead of the real association. &lt;/p&gt;

&lt;p&gt;Then, came the edit page, the part containing forms...&lt;/p&gt;

&lt;p&gt;After using Rails for years, there is still one thing I always have "difficulties" to play with: the nested resources/forms. Each time I have to play with it, I have to check the documentation again and again, like if it was the first time I'm doing it.&lt;/p&gt;

&lt;p&gt;In that case of soft deletion, the main concern I had was to find a way to modify the magic of Rails about nested resource deletion. The easier thing seemed to monkey patch the destroy method in my model.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3o85xAYQLOhSrmINHO/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3o85xAYQLOhSrmINHO/giphy.gif"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;destroy&lt;/span&gt;
  &lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;deleted_at: &lt;/span&gt;&lt;span class="no"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It worked, but I know deep inside that someday I would regret that change. I really needed this &lt;code&gt;destroy&lt;/code&gt; new behaviour in the context of a deletion from Active Admin only. So I continued to check the code of the app to finally found an interesting line in &lt;em&gt;models/order_payments.rb&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;controller&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update&lt;/span&gt;
    &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ss"&gt;:update_from_admin_space&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/3o6Mbj2w67HnPQKgcE/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3o6Mbj2w67HnPQKgcE/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I just had to add a condition to my &lt;code&gt;destroy&lt;/code&gt; method to use soft deletion only in the case of an update from the admin space. I'm still not fully satisfied, but it seems better than my initial solution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;destroy&lt;/span&gt;
  &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;context&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="ss"&gt;:update_from_admin_space&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;deleted_at: &lt;/span&gt;&lt;span class="no"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So from there, my record was soft deleted as planned, and I was able to only show the data that were not softly deleted. Nevertheless, I had another trouble. When I tested the edit page, the soft deleted resources were still showing. So I added a condition to no show the soft deleted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inputs&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:order_payments&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;new_record: &lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;payment_accepted?&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;allow_destroy: &lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;validated?&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;order_payment&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;order_payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deleted_at&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;blank?&lt;/span&gt;
      &lt;span class="n"&gt;order_payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt; &lt;span class="ss"&gt;:expected_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But was actually not enough. Yes, my record wasn't showing, but that &lt;code&gt;allow_destroy&lt;/code&gt; magically made the checkbox appear for each soft deleted record...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/ywH9QD9OhPAUnZ13kJ/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ywH9QD9OhPAUnZ13kJ/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After some time, I checked the Active Admin documentation and a line sounded promising:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is possible to associate :allow_destroy with a string or a symbol, corresponding to the name of a child object’s method that will get called, or with a Proc object.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It was clear, I just had to move that &lt;code&gt;allow_destroy&lt;/code&gt; condition somewhere and change the rule to return &lt;code&gt;false&lt;/code&gt; whenever I'm dealing with a soft deleted record:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;deletable_from_admin&lt;/span&gt;
  &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;validated?&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;deleted_at&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;blank?&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, everything works as expected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/NEvPzZ8bd1V4Y/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/NEvPzZ8bd1V4Y/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I know my solution is probably not the best, but it works. Did you also have this kind of problem? How did you solve it? I'm curious. 🙃&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>programming</category>
    </item>
    <item>
      <title>About Me</title>
      <dc:creator>Gaëtan Masson</dc:creator>
      <pubDate>Thu, 30 Aug 2018 20:45:56 +0000</pubDate>
      <link>https://forem.com/gaetanm/about-me-30di</link>
      <guid>https://forem.com/gaetanm/about-me-30di</guid>
      <description>&lt;p&gt;&lt;em&gt;This is a "repost" of the initial "Hi, I'm Gaëtan Masson" that I decided to redo because many things changed (like, I'm not a student anymore).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I have been coding for many years (I started when I was a teenager) but only for few years in a serious way. I started coding on my spare time with the C language then I went into web programming with PHP. I have to admit that I also flirted with Python at that period (my first program was a port scanner, what a hacker!).&lt;/p&gt;

&lt;p&gt;In CS school, we did low level stuff by reinventing the wheel with the C language (had to rewrite malloc, printf...) and even a bit of Assembly. Then we went into OOP with C++ and Java. At the end, I did some Swift (had to create an iOS app) for the final school project.&lt;/p&gt;

&lt;p&gt;Today, I mostly program in Ruby (I make my living as Ruby on Rails developer) but I have a strong interest toward Elixir (what a surprise).&lt;/p&gt;

&lt;p&gt;About my hobbies, I enjoy to do weight lifting and being lazy by doing nerd stuff like watching animes, playing video games and hugging my two cats.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzf1ynoml3ak23itz643j.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzf1ynoml3ak23itz643j.jpg" alt="Kira and Ruyk" width="800" height="1066"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I also like to read/watch/listen stuff about science in general, technology and psychology (the brain is an interesting machine, don't you find?).&lt;/p&gt;

&lt;p&gt;I'm currently living in Lille (France). You can find me on GitHub as &lt;a href="https://github.com/gaetanm" rel="noopener noreferrer"&gt;gaetanm&lt;/a&gt; and on Twitter as &lt;a href="https://twitter.com/gaetanm_" rel="noopener noreferrer"&gt;@gaetanm_&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To finish, here is a photo of &lt;a href="https://fr.wikipedia.org/wiki/Yukihiro_Matsumoto" rel="noopener noreferrer"&gt;Matz&lt;/a&gt; with me!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fynpo5idsk6v5qsd0m67r.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fynpo5idsk6v5qsd0m67r.jpg" alt="Matz and me" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nice to meet you.&lt;/p&gt;

</description>
      <category>introduction</category>
    </item>
    <item>
      <title>Getting Back to an Old PostgreSQL Specific Version</title>
      <dc:creator>Gaëtan Masson</dc:creator>
      <pubDate>Thu, 16 Aug 2018 09:26:17 +0000</pubDate>
      <link>https://forem.com/gaetanm/getting-back-to-an-old-postgressql-specific-version-3o01</link>
      <guid>https://forem.com/gaetanm/getting-back-to-an-old-postgressql-specific-version-3o01</guid>
      <description>&lt;h1&gt;
  
  
  TL;DR
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Exemple about getting back to 9.6.6 from 9.6.7
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew services stop postgresql 
brew services stop postgresql@9.6

brew uninstall postgresql 
brew uninstall postgresql@9.6

&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /usr/local/var/postgres .psql_history .psqlrc .psql.local .pgpass .psqlrc.local
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Select the Postgres.app release having the desired version &lt;a href="https://github.com/PostgresApp/PostgresApp/releases/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Install it, "Initialize" the server then run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /etc/paths.d &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; /Applications/Postgres.app/Contents/Versions/9.6/bin | &lt;span class="nb"&gt;sudo tee&lt;/span&gt; /etc/paths.d/postgresapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reboot the shell.&lt;/p&gt;

&lt;h1&gt;
  
  
  Details
&lt;/h1&gt;

&lt;p&gt;The PostgreSQL version of the project I'm working on at work is &lt;code&gt;9.6.6&lt;/code&gt;. The problem is that my machine uses a later version. It results in conflicts with the &lt;code&gt;db/structure&lt;/code&gt; file each time I run a &lt;code&gt;db:migrate&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Like most dev running on macOS, I usually use Homebrew. It allows to easily switch to another installed version of a formula by running &lt;code&gt;brew switch formula@version&lt;/code&gt;. As you guess I tried, but the problem is that Homebrew only handles major versions of each release and I need a specific &lt;strong&gt;minor&lt;/strong&gt; version.&lt;/p&gt;

&lt;p&gt;Because I also need to switch between old and recent Rails projects, I often have to change the running PostgreSQL version. My current solution is to use the &lt;a href="https://postgresapp.com" rel="noopener noreferrer"&gt;Postgres.app&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2n8ga9xmacexuwj1ldop.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2n8ga9xmacexuwj1ldop.png" alt="Alt Postgres.app" width="713" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you already have PostgreSQL installed on your system via Homebrew but want to use a specific version (like a previous one) with Postgres.app, just follow these steps:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Remove all PostgreSQL version installed
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Check running version &lt;code&gt;brew services list&lt;/code&gt; then stop them &lt;code&gt;brew services stop postgresql&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Delete all installed formulas with &lt;code&gt;brew uninstall postgresql&lt;/code&gt; and &lt;code&gt;brew uninstall postgresql@version&lt;/code&gt; per installed version.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then delete the related files &lt;code&gt;rm -rf /usr/local/var postgres .psql_history .psqlrc .psql.local .pgpass .psqlrc.local&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Install Postgres.app
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/PostgresApp/PostgresApp/releases/" rel="noopener noreferrer"&gt;Download the release&lt;/a&gt; having the desired version.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install it and create a new server:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd81quafaktmbvi1n6nkq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd81quafaktmbvi1n6nkq.png" alt="Alt Postgres.app" width="713" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialize it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Make PostgreSQL related tools usable with the terminal
&lt;/h2&gt;

&lt;p&gt;This last step allows you to use tools like pg_dump etc. Just run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /etc/paths.d &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; /Applications/Postgres.app/Contents/Versions/9.6/bin | &lt;span class="nb"&gt;sudo tee&lt;/span&gt; /etc/paths.d/postgresapp&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Don't forget to replace 9.6 with your version).&lt;/p&gt;

&lt;p&gt;Relaunch your shell. Welcome back to the past!&lt;/p&gt;

&lt;p&gt;If you have better solution to switch between specific versions of PostgreSQL without external app like I did, don't hesitate to share it! :)&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>postgres</category>
    </item>
    <item>
      <title>Check If a File Was Uploaded with CarrierWave</title>
      <dc:creator>Gaëtan Masson</dc:creator>
      <pubDate>Tue, 14 Aug 2018 14:47:49 +0000</pubDate>
      <link>https://forem.com/gaetanm/check-if-a-file-was-uploaded-with-carrierwave-1h5l</link>
      <guid>https://forem.com/gaetanm/check-if-a-file-was-uploaded-with-carrierwave-1h5l</guid>
      <description>&lt;h1&gt;
  
  
  TL;DR
&lt;/h1&gt;

&lt;p&gt;Use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;present?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;NOT:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nil?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Details
&lt;/h1&gt;

&lt;p&gt;When you want to check if a file is attached to your model, the official Carrierwave documentation says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: &lt;code&gt;u.avatar&lt;/code&gt; will never return nil, even if there is no photo associated to it. To check if a photo was saved to the model, use &lt;code&gt;u.avatar.file.nil?&lt;/code&gt; instead.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's fine, it works. BUT! If you are using a cloud storage service like Amazon S3, each time the line &lt;code&gt;file.nil?&lt;/code&gt; will be executed, a request will be sent to the cloud storage to check the presence of the file. As you guess, it's not cool because it can make your app slower.&lt;/p&gt;

&lt;p&gt;The solution is simply to use &lt;code&gt;u.avatar.present?&lt;/code&gt;. This way, no more external call.&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>rails</category>
      <category>carrierwave</category>
      <category>ruby</category>
    </item>
  </channel>
</rss>
