<?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>DEV Community: Takehiro_Yamazaki</title>
    <description>The latest articles on DEV Community by Takehiro_Yamazaki (@take0420).</description>
    <link>https://dev.to/take0420</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%2F1467919%2Ffda31904-3916-48f4-81b6-b85410a0e696.png</url>
      <title>DEV Community: Takehiro_Yamazaki</title>
      <link>https://dev.to/take0420</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/take0420"/>
    <language>en</language>
    <item>
      <title>Rails Meets GraphQL: How to Master context and find_by with Examples</title>
      <dc:creator>Takehiro_Yamazaki</dc:creator>
      <pubDate>Sun, 23 Mar 2025 05:33:00 +0000</pubDate>
      <link>https://dev.to/take0420/rails-meets-graphql-how-to-master-context-and-findby-with-examples-3ip3</link>
      <guid>https://dev.to/take0420/rails-meets-graphql-how-to-master-context-and-findby-with-examples-3ip3</guid>
      <description>&lt;h2&gt;
  
  
  Introduce
&lt;/h2&gt;

&lt;p&gt;Ever wondered how to fetch data in Rails with GraphQL without overcomplicating things? You’ve got two trusty tools: &lt;code&gt;context&lt;/code&gt; and &lt;code&gt;find_by&lt;/code&gt;. Choosing the right one can make your code cleaner, your queries faster, and your app easier to maintain.&lt;/p&gt;

&lt;p&gt;In this guide, I’ll break down their differences, walk you through real examples, and share some lessons learned from working with both in production.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;strong&gt;Note&lt;/strong&gt;: This guide is based on my own experience. Depending on your project’s architecture, there might be some nuances or edge cases not covered here.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🔍 What’s the Deal with &lt;code&gt;context&lt;/code&gt; and &lt;code&gt;find_by&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;Let’s start with the basics—what each one actually does.&lt;/p&gt;

&lt;h3&gt;
  
  
  🕵️‍♂️ &lt;code&gt;find_by&lt;/code&gt;: The Database Detective
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Searches the database based on given conditions.&lt;/li&gt;
&lt;li&gt;Executes a fresh query every time—no caching.&lt;/li&gt;
&lt;li&gt;Returns &lt;code&gt;nil&lt;/code&gt; if no match (use &lt;code&gt;find_by!&lt;/code&gt; to raise an error instead).&lt;/li&gt;
&lt;li&gt;Doesn’t rely on model relationships—works anywhere.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🤝 &lt;code&gt;context&lt;/code&gt;: The Request Sidekick
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Stores temporary data shared across a GraphQL request.&lt;/li&gt;
&lt;li&gt;Holds things like &lt;code&gt;context[:current_user]&lt;/code&gt; for quick access.&lt;/li&gt;
&lt;li&gt;Helps avoid redundant database calls by reusing already-fetched data.&lt;/li&gt;
&lt;li&gt;Especially useful for accessing data tied to the logged-in user.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ When &lt;code&gt;context&lt;/code&gt; Saves the Day
&lt;/h2&gt;

&lt;p&gt;If you're fetching data tied directly to the authenticated user, &lt;code&gt;context&lt;/code&gt; is your best friend.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example: Fetching a User Profile&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# File: app/graphql/types/auth_query_type.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Auth&lt;/span&gt;
  &lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Types&lt;/span&gt;
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AuthQueryType&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Base&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Types&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;BaseObject&lt;/span&gt;
      &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ss"&gt;:user_profile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Auth&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Types&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ProfileType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;null: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="ss"&gt;description: &lt;/span&gt;&lt;span class="s2"&gt;"Gets the current user's profile."&lt;/span&gt;

      &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;user_profile&lt;/span&gt;
        &lt;span class="c1"&gt;# Grabs the profile via the user’s relationship—no extra query needed&lt;/span&gt;
        &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:current_user&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;profile&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&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;strong&gt;Why it works:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Since &lt;code&gt;context[:current_user]&lt;/code&gt; is already loaded, calling &lt;code&gt;.profile&lt;/code&gt; just uses the ActiveRecord association—no additional database hit. This is perfect for performance-sensitive routes.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔎 When &lt;code&gt;find_by&lt;/code&gt; Takes the Lead
&lt;/h2&gt;

&lt;p&gt;Sometimes you need more control—specific filters, IDs, or permissions. That’s where &lt;code&gt;find_by&lt;/code&gt; comes in.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example: Finding a Post by ID&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# File: app/graphql/types/auth_query_type.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Auth&lt;/span&gt;
  &lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Types&lt;/span&gt;
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AuthQueryType&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Base&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Types&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;BaseObject&lt;/span&gt;
      &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ss"&gt;:post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Auth&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Types&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;PostType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;null: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="n"&gt;argument&lt;/span&gt; &lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;required: &lt;/span&gt;&lt;span class="kp"&gt;true&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;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:)&lt;/span&gt;
        &lt;span class="c1"&gt;# Finds a post matching the ID and user, throws an error if not found&lt;/span&gt;
        &lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_by!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;user_id: &lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:current_user&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&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;strong&gt;Why use &lt;code&gt;find_by!&lt;/code&gt;?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
You're filtering not just by ID, but also by &lt;code&gt;user_id&lt;/code&gt;—something relationships alone can't express. And with &lt;code&gt;find_by!&lt;/code&gt;, you make sure an error is raised if the post doesn’t exist. (Use &lt;code&gt;find_by&lt;/code&gt; if you’re okay with getting &lt;code&gt;nil&lt;/code&gt;.)&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 Your Cheat Sheet for Choosing
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Scenario&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;context&lt;/code&gt; ✅&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;find_by&lt;/code&gt; ✅&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Fetching logged-in user data&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Searching by a specific ID&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Accessing data via relationships&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Adding custom filters&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Handling records that might be nil&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Rule of thumb:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Use &lt;code&gt;context&lt;/code&gt; for relationship-driven access. Use &lt;code&gt;find_by&lt;/code&gt; when you need fine-grained filters or extra logic.&lt;/p&gt;




&lt;h2&gt;
  
  
  💭 My Two Cents from the Trenches
&lt;/h2&gt;

&lt;p&gt;After using both in real-world Rails + GraphQL apps, here’s how I approach the decision:&lt;/p&gt;

&lt;h3&gt;
  
  
  🔄 Why I Reach for &lt;code&gt;context&lt;/code&gt;
&lt;/h3&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;profile&lt;/span&gt;
  &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:current_user&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;profile&lt;/span&gt; &lt;span class="c1"&gt;# Clean, quick, done&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the data is tied to a user and you already have the user loaded, &lt;code&gt;context&lt;/code&gt; gives you a fast, elegant solution. I’ve seen it reduce database queries significantly.&lt;/p&gt;

&lt;h3&gt;
  
  
  🔍 Why &lt;code&gt;find_by&lt;/code&gt; Still Has Its Place
&lt;/h3&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;published_post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post_id&lt;/span&gt;&lt;span class="p"&gt;:)&lt;/span&gt;
  &lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="n"&gt;post_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;status: &lt;/span&gt;&lt;span class="s1"&gt;'published'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Filters like a champ&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Need filters? Want to find data that may or may not exist? Dealing with permissions? That’s &lt;code&gt;find_by&lt;/code&gt; territory.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 Wrapping Up: Your Action Plan
&lt;/h2&gt;

&lt;p&gt;Here’s how to apply what you’ve learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use &lt;code&gt;context[:current_user]&lt;/code&gt;&lt;/strong&gt; when working with logged-in user relationships.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use &lt;code&gt;find_by&lt;/code&gt;&lt;/strong&gt; when:

&lt;ul&gt;
&lt;li&gt;You need to filter with custom conditions.&lt;/li&gt;
&lt;li&gt;You're fetching another user’s data (with proper permission checks).&lt;/li&gt;
&lt;li&gt;A record might not exist and you need graceful handling.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Mastering this distinction keeps your codebase clean, performant, and maintainable.&lt;/p&gt;




&lt;h2&gt;
  
  
  📚 Further Reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://graphql-ruby.org/queries/executing_queries" rel="noopener noreferrer"&gt;GraphQL Ruby — Executing Queries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://guides.rubyonrails.org/association_basics.html" rel="noopener noreferrer"&gt;Rails Guides — Active Record Associations&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
      <category>graphql</category>
    </item>
    <item>
      <title>【Docker &amp; Rails】How to Securely Modify Database Migrations 🐳</title>
      <dc:creator>Takehiro_Yamazaki</dc:creator>
      <pubDate>Tue, 18 Mar 2025 02:57:24 +0000</pubDate>
      <link>https://dev.to/take0420/docker-rails-how-to-securely-modify-database-migrations-5754</link>
      <guid>https://dev.to/take0420/docker-rails-how-to-securely-modify-database-migrations-5754</guid>
      <description>&lt;h2&gt;
  
  
  How to Safely Update Rails Migrations in a Docker Environment
&lt;/h2&gt;

&lt;p&gt;In Rails development, you may occasionally need to change the data type of a column after running migrations.&lt;/p&gt;

&lt;p&gt;This article explains how to safely alter table structures in a Rails application within a Docker environment. We’ll demonstrate this by changing the &lt;code&gt;price&lt;/code&gt; column in a &lt;code&gt;Books&lt;/code&gt; table from &lt;code&gt;string&lt;/code&gt; to &lt;code&gt;integer&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Initial Table Structure
&lt;/h3&gt;

&lt;p&gt;Consider the following migration has already been executed:&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;class&lt;/span&gt; &lt;span class="nc"&gt;CreateBooks&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;7.1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
    &lt;span class="n"&gt;create_table&lt;/span&gt; &lt;span class="ss"&gt;:books&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;t&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:price&lt;/span&gt;

      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timestamps&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, you might later realize you need numerical handling for price values.&lt;/p&gt;




&lt;h3&gt;
  
  
  Recommended Approach (Creating a New Migration)
&lt;/h3&gt;

&lt;p&gt;Rails recommends adding a new migration instead of directly modifying existing ones.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1: Generate a new migration file in Docker
&lt;/h4&gt;

&lt;p&gt;In Docker environments, Rails commands should be run inside the container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose &lt;span class="nb"&gt;exec &lt;/span&gt;web bundle &lt;span class="nb"&gt;exec &lt;/span&gt;rails generate migration ChangeBooksPriceToInteger
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a new migration file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db/migrate/20250314000000_change_books_price_to_integer.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 2: Edit the migration file
&lt;/h4&gt;

&lt;p&gt;Open the generated migration file and define the type change clearly:&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;class&lt;/span&gt; &lt;span class="nc"&gt;ChangeBooksPriceToInteger&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;7.1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;up&lt;/span&gt;
    &lt;span class="n"&gt;change_column&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:integer&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;down&lt;/span&gt;
    &lt;span class="n"&gt;change_column&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:string&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;up method&lt;/strong&gt;: Defines changes applied when running migrations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;down method&lt;/strong&gt;: Defines changes applied during rollbacks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 3: Run the migration in Docker
&lt;/h4&gt;

&lt;p&gt;Apply the changes to the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose &lt;span class="nb"&gt;exec &lt;/span&gt;web bundle &lt;span class="nb"&gt;exec &lt;/span&gt;rails db:migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the &lt;code&gt;price&lt;/code&gt; column of the &lt;code&gt;Books&lt;/code&gt; table has been updated to the &lt;code&gt;integer&lt;/code&gt; type.&lt;/p&gt;




&lt;h3&gt;
  
  
  Important Notes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Always verify that changing column types won't cause data inconsistency in environments with existing data, especially in production.&lt;/li&gt;
&lt;li&gt;For non-critical data in development environments, rolling back and re-running the initial migration is possible, but adding a new migration is safer for production systems.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;In Docker environments, always run Rails migration commands inside the container.&lt;/li&gt;
&lt;li&gt;Safely change Rails migrations by adding new migration files rather than altering existing ones.&lt;/li&gt;
&lt;li&gt;Clearly define changes and rollbacks using &lt;code&gt;up&lt;/code&gt; and &lt;code&gt;down&lt;/code&gt; methods.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This summarizes how to safely update existing Rails migrations in a Docker environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://guides.rubyonrails.org/active_record_migrations.html#migration-overview" rel="noopener noreferrer"&gt;Active Record Migrations&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
      <category>productivity</category>
    </item>
    <item>
      <title>How to Toggle Auto-Completion in Cursor Editor 🎉</title>
      <dc:creator>Takehiro_Yamazaki</dc:creator>
      <pubDate>Tue, 26 Nov 2024 13:31:44 +0000</pubDate>
      <link>https://dev.to/take0420/how-to-toggle-auto-completion-in-cursor-editor-12il</link>
      <guid>https://dev.to/take0420/how-to-toggle-auto-completion-in-cursor-editor-12il</guid>
      <description>&lt;h1&gt;
  
  
  Background
&lt;/h1&gt;

&lt;p&gt;Around November 2024, the Cursor Editor was updated to include Auto-Completion. This is a fantastic feature, but sometimes you might want to disable it temporarily. This guide explains how to do just that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.cursor.com/" rel="noopener noreferrer"&gt;https://www.cursor.com/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;p&gt;This guide is for MacBook users.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps to Disable Auto-Completion
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Open the Cursor Editor and press cmd + shift + p.&lt;/li&gt;
&lt;li&gt;Type "disable cursor tab".&lt;/li&gt;
&lt;li&gt;Press Enter to apply the change.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Before
&lt;/h3&gt;

&lt;p&gt;When auto-completion is enabled:&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%2F80jfhzic3b2xe4hgd2jb.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%2F80jfhzic3b2xe4hgd2jb.png" alt="Enable auto-completion" width="328" height="105"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  After
&lt;/h3&gt;

&lt;p&gt;Once you type "disable cursor tab" and press Enter, auto-completion will be disabled:&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%2F18asi7m0z25x9ynutxv0.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%2F18asi7m0z25x9ynutxv0.png" alt="Disble auto-completion" width="311" height="111"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Re-Enable Auto-Completion
&lt;/h2&gt;

&lt;p&gt;To turn auto-completion back on:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open the Cursor Editor and press cmd + shift + p.&lt;/li&gt;
&lt;li&gt;Type "enable cursor tab".&lt;/li&gt;
&lt;li&gt;Press Enter to apply the change.&lt;/li&gt;
&lt;/ol&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%2F0zhsztbkzow1adrwncop.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%2F0zhsztbkzow1adrwncop.png" alt="ReEnable auto-completion" width="744" height="98"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://forum.cursor.com/t/quick-disable-enable/15587/4" rel="noopener noreferrer"&gt;https://forum.cursor.com/t/quick-disable-enable/15587/4&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>ai</category>
      <category>cursor</category>
    </item>
    <item>
      <title>🎨 CI/CD Workflow with AWS and Docker</title>
      <dc:creator>Takehiro_Yamazaki</dc:creator>
      <pubDate>Tue, 14 May 2024 01:10:04 +0000</pubDate>
      <link>https://dev.to/take0420/cicd-workflow-with-aws-and-docker-38i3</link>
      <guid>https://dev.to/take0420/cicd-workflow-with-aws-and-docker-38i3</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hello, I'm &lt;a href="https://dev.to/take0420/self-introduction-im-new-to-devto-21kb"&gt;Take&lt;/a&gt;, an engineer working at a product development company in Tokyo.&lt;/p&gt;

&lt;p&gt;In this article, I will illustrate the overall structure of a CI/CD workflow utilizing AWS ECR/ECS, Docker, and GitHub Actions. I won't delve into specific setup steps but will focus on how these technologies are integrated and provide a visual explanation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Technologies Used
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;VSCode: Editor for coding&lt;/li&gt;
&lt;li&gt;GitHub Actions: Automates builds and tests upon code changes&lt;/li&gt;
&lt;li&gt;Docker: Tool for containerizing applications&lt;/li&gt;
&lt;li&gt;AWS ECR (Elastic Container Registry): Manages Docker container images&lt;/li&gt;
&lt;li&gt;AWS ECS (Elastic Container Service): Manages containers and orchestrates them&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Overall Process (tool:&lt;a href="https://excalidraw.com/" rel="noopener noreferrer"&gt;Excalidraw&lt;/a&gt;)
&lt;/h2&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%2F4ps8fhdonenf0c54cpkp.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%2F4ps8fhdonenf0c54cpkp.png" alt="Overall Process" width="800" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Developers make changes in VSCode and push them to GitHub.&lt;/li&gt;
&lt;li&gt;GitHub Actions triggers, executing builds and tests, and uses Docker to create container images.&lt;/li&gt;
&lt;li&gt;These images are stored in AWS ECR and then deployed via AWS ECS.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Detailed Explanation by Part
&lt;/h2&gt;

&lt;h3&gt;
  
  
  From Code Push to GitHub Actions
&lt;/h3&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%2F0cjwayzoxkkv8v9o82vd.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%2F0cjwayzoxkkv8v9o82vd.png" alt="From Code Push to GA" width="763" height="590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Developers push changes from their local branch to the GitHub repository with the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"commit message"&lt;/span&gt;
git push origin HEAD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This triggers GitHub Actions, which automatically performs builds and tests, during which Docker creates a container image.&lt;/p&gt;

&lt;h3&gt;
  
  
  From &lt;a href="https://docs.github.com/en/actions" rel="noopener noreferrer"&gt;GitHub Actions&lt;/a&gt; to AWS ECR
&lt;/h3&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%2F4aut498zrhl125fggvu0.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%2F4aut498zrhl125fggvu0.png" alt="From GA to AWS ECR" width="700" height="488"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the build and tests succeed, the Docker image is uploaded to AWS ECR.&lt;/p&gt;

&lt;p&gt;Think of ECR as a service that manages Docker images, offering secure access control and private networking. It’s a vital part of automating and securing the workflow from development to deployment.&lt;/p&gt;

&lt;h3&gt;
  
  
  From AWS &lt;a href="https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html" rel="noopener noreferrer"&gt;ECR&lt;/a&gt; to ECS
&lt;/h3&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%2Feqd2gf6w9ax6ko346q0w.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%2Feqd2gf6w9ax6ko346q0w.png" alt="From AWS ECR to ECS" width="800" height="719"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Docker image saved in ECR is then pulled by ECS, executing necessary initializations through an &lt;code&gt;entrypoint.sh&lt;/code&gt; script.&lt;/p&gt;

&lt;p&gt;"Pull" refers to the process of ECS retrieving container images from the ECR to start containers, managing applications with precise version control.&lt;/p&gt;

&lt;h3&gt;
  
  
  From &lt;a href="https://docs.aws.amazon.com/ecs/" rel="noopener noreferrer"&gt;ECS&lt;/a&gt; to Application Deployment
&lt;/h3&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%2Fkpeg0mpacb6vgr2w1chx.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%2Fkpeg0mpacb6vgr2w1chx.png" alt="From ECS to Application Deployment" width="389" height="582"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This section focuses on the concept of "kick," which in ECS terms, means a specific task has started running. &lt;/p&gt;

&lt;p&gt;It's a step where the new task instances are launched based on task definitions, deploying the container images as live containers, ensuring the application operates as expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Thank you for reading this far! If you enjoyed this article, I'd really appreciate it if you could give it a 'Like' 🎉&lt;/p&gt;

</description>
      <category>docker</category>
      <category>aws</category>
      <category>programming</category>
      <category>githubactions</category>
    </item>
    <item>
      <title>💎 Using Ruby's SecureRandom to Strengthen User Authentication</title>
      <dc:creator>Takehiro_Yamazaki</dc:creator>
      <pubDate>Sun, 12 May 2024 13:47:42 +0000</pubDate>
      <link>https://dev.to/take0420/using-rubys-securerandom-to-strengthen-user-authentication-he3</link>
      <guid>https://dev.to/take0420/using-rubys-securerandom-to-strengthen-user-authentication-he3</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hi, I'm &lt;a href="https://dev.to/take0420/self-introduction-im-new-to-devto-21kb"&gt;Take&lt;/a&gt;, and I work as an engineer at an in-house development company in Tokyo. &lt;/p&gt;

&lt;p&gt;In this article, I'll share what I've learned about generating tokens necessary for secure user authentication using Ruby’s standard library, SecureRandom.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is SecureRandom?
&lt;/h3&gt;

&lt;p&gt;SecureRandom is a tool used to generate random numbers and strings that are hard to predict. You can learn more about it in this helpful article &lt;a href="https://api.rubyonrails.org/classes/SecureRandom.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Token?
&lt;/h3&gt;

&lt;p&gt;A token is a randomly generated string used temporarily to identify a user.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Digest?
&lt;/h3&gt;

&lt;p&gt;During authentication, the data sent is transformed using the same hash function and verified against the stored digest, which is kept on the server side.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparing Tokens and Digests
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Token&lt;/th&gt;
&lt;th&gt;Digest&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Purpose&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Temporarily stored in the user's browser and used directly for authentication.&lt;/td&gt;
&lt;td&gt;Stored in the database and used for token verification.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Generation Method&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Generated using random data functions like &lt;code&gt;SecureRandom.urlsafe_base64&lt;/code&gt;.&lt;/td&gt;
&lt;td&gt;Generated from the token using hash functions like &lt;code&gt;SHA-256&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Security&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Randomly generated, but can be misused if stolen.&lt;/td&gt;
&lt;td&gt;Stored in a hashed form, making it difficult to identify the original token if stolen.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&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%2Ftnbe4cvaqfb7qvxevuvu.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%2Ftnbe4cvaqfb7qvxevuvu.png" alt="Golden Security" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Generating a Token
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;SecureRandom.urlsafe_base64&lt;/code&gt; method generates a safe, random token. This method produces a 22-character string from a set of 64 possible characters (A-Z, a-z, 0-9, "-", "_").&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;irb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="mo"&gt;001&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;SecureRandom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlsafe_base64&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"TGYseMGxXvuG4tmD08MiAQ"&lt;/span&gt;
&lt;span class="n"&gt;irb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="mo"&gt;002&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;SecureRandom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlsafe_base64&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"a_xN8Hw0BMuRHrGszl-CLA"&lt;/span&gt;
&lt;span class="n"&gt;irb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="mo"&gt;003&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;SecureRandom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlsafe_base64&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"AUMBKxwWbV0eMGGEP2LNJg"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Generating a Digest
&lt;/h2&gt;

&lt;p&gt;The token generated is hashed using the User.digest method, and this hashed version (digest) is stored in the database. This makes it difficult to identify the actual token if it were ever leaked.&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;remember&lt;/span&gt;
  &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remember_token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new_token&lt;/span&gt;
  &lt;span class="n"&gt;update_attribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:remember_digest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;remember_token&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;This method generates a new token and saves it to the remember_token property of the user instance before updating the token’s digest in the remember_digest field.&lt;/p&gt;

&lt;h2&gt;
  
  
  User Authentication Process
&lt;/h2&gt;

&lt;p&gt;When a user revisits the site, the token saved in the cookies is retrieved, hashed, and verified against the digest stored in the database. This process authenticates the user.&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;rails&lt;/span&gt; &lt;span class="n"&gt;console&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;sandbox&lt;/span&gt;
&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;
&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remember&lt;/span&gt;
  &lt;span class="no"&gt;TRANSACTION&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="no"&gt;SAVEPOINT&lt;/span&gt; &lt;span class="n"&gt;active_record_1&lt;/span&gt;
  &lt;span class="no"&gt;User&lt;/span&gt; &lt;span class="no"&gt;Update&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="no"&gt;UPDATE&lt;/span&gt; &lt;span class="s2"&gt;"users"&lt;/span&gt; &lt;span class="no"&gt;SET&lt;/span&gt; &lt;span class="s2"&gt;"updated_at"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;?,&lt;/span&gt; &lt;span class="s2"&gt;"remember_digest"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="no"&gt;WHERE&lt;/span&gt; &lt;span class="s2"&gt;"users"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt;  &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="s2"&gt;"updated_at"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2024-05-12 05:18:18.739100"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"remember_digest"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$2a$12$RQayRzS/lv5Je8NDcycI9ut2uMn8uNDMWUZ.H0t1ixBXyRHFn6mYS"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
  &lt;span class="no"&gt;TRANSACTION&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="no"&gt;RELEASE&lt;/span&gt; &lt;span class="no"&gt;SAVEPOINT&lt;/span&gt; &lt;span class="n"&gt;active_record_1&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Through this article, we've explored secure methods of user authentication using tokens and digests, with a focus on SecureRandom for generating safe tokens. Understanding this foundation is crucial for further strengthening security measures.&lt;/p&gt;

&lt;p&gt;Thank you for reading! If you liked this article, please give it a "like" 🎉. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>【Self-Introduction】I'm new to "Dev.To" 📚</title>
      <dc:creator>Takehiro_Yamazaki</dc:creator>
      <pubDate>Sat, 04 May 2024 08:10:47 +0000</pubDate>
      <link>https://dev.to/take0420/self-introduction-im-new-to-devto-21kb</link>
      <guid>https://dev.to/take0420/self-introduction-im-new-to-devto-21kb</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hello, nice to meet you! My name is Take. &lt;strong&gt;I work as an software engineer at HR development company in Tokyo,Japan&lt;/strong&gt;. 🇯🇵 &lt;/p&gt;

&lt;p&gt;From now on, I plan to write articles on "dev.to" to share what I've learned and discuss my career as an engineer.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Career
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Graduated from a university in Tokyo in March 2022.&lt;/li&gt;
&lt;li&gt;Started working as a national government official in April 2022.&lt;/li&gt;
&lt;li&gt;Resigned in February 2023.&lt;/li&gt;
&lt;li&gt;Currently working(in-house HR development in Tokyo)since March 2023.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why I started using "dev.to"
&lt;/h2&gt;

&lt;p&gt;There are 3 main reasons.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I want to keep a record of my career, including my daily experiences and learning.&lt;/li&gt;
&lt;li&gt;To challenge myself by writing in English.&lt;/li&gt;
&lt;li&gt;It's a globally recognized tech platform.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Additionally, I have a preference of the simple UI of "dev.to"&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Here's what I'm currently working on:
&lt;/h2&gt;

&lt;p&gt;I work as an software engineer at an in-house development HR company in Tokyo.&lt;/p&gt;

&lt;h3&gt;
  
  
  Main Tech Stack
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Ruby&lt;/li&gt;
&lt;li&gt;React &lt;/li&gt;
&lt;li&gt;AWS&lt;/li&gt;
&lt;li&gt;Terraform&lt;/li&gt;
&lt;li&gt;Docker&lt;/li&gt;
&lt;li&gt;Datadog&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I'll write about on "dev.to"
&lt;/h2&gt;

&lt;p&gt;Here are some of the themes I plan to tackle on "dev.to".&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sharing what I learn about engineering.&lt;/li&gt;
&lt;li&gt;Discussions on career development.&lt;/li&gt;
&lt;li&gt;Everyday life in Tokyo&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;I'll casually share things I've actually experienced myself.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you for your continued support!
&lt;/h2&gt;

&lt;p&gt;I'll strive to keep my posts short and clear. Please continue to follow and enjoy my updates.&lt;/p&gt;

&lt;p&gt;That's all.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>productivity</category>
      <category>discuss</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
