<?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: Hardik Radadiya</title>
    <description>The latest articles on DEV Community by Hardik Radadiya (@hardik355).</description>
    <link>https://dev.to/hardik355</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%2F241005%2Fe8d21d06-f559-4ad5-95b4-a77f1562d02f.png</url>
      <title>DEV Community: Hardik Radadiya</title>
      <link>https://dev.to/hardik355</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hardik355"/>
    <language>en</language>
    <item>
      <title>Rails Database Migrations: Tips, Tricks, and Best Practices</title>
      <dc:creator>Hardik Radadiya</dc:creator>
      <pubDate>Tue, 03 Oct 2023 09:09:49 +0000</pubDate>
      <link>https://dev.to/hardik355/rails-database-migrations-tips-tricks-and-best-practices-1je6</link>
      <guid>https://dev.to/hardik355/rails-database-migrations-tips-tricks-and-best-practices-1je6</guid>
      <description>&lt;p&gt;If you’re a Ruby on Rails developer, you’re probably no stranger to the concept of migrations. Migrations are an essential part of managing your application’s database schema, allowing you to make changes to your database structure in an organized and version-controlled manner. In this guide, we’ll dive deep into migrations in Rails, exploring various scenarios and commands that will help you wield the power of migrations effectively.&lt;/p&gt;

&lt;p&gt;Adding Columns to Existing Tables&lt;br&gt;
&lt;strong&gt;Scenario 1: Adding a Column with an Index&lt;/strong&gt;&lt;br&gt;
Let’s say you want to add an ‘email’ column to your ‘users’ table and index it for faster search operations. Here’s how you can do it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate migration AddEmailToUsers email:string:index
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command generates a migration file, and you can add the following code to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def change
  add_column :users, :email, :string
  add_index :users, :email
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This migration adds the ‘email’ column to the ‘users’ table and creates an index on it for improved query performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario 2: Adding Multiple Columns&lt;/strong&gt;&lt;br&gt;
Adding multiple columns to a table is also straightforward. Suppose you want to add ‘name,’ ‘salary,’ and ‘email’ columns to the ‘users’ table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate migration AddDetailsToUsers name:string salary:decimal email:string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The generated migration file will include code to add all three columns to the ‘users’ table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modifying Columns&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Scenario 3: Renaming a Column&lt;/strong&gt;&lt;br&gt;
To rename a column in a table, you can use a migration. Let’s say you want to rename a column named ‘old_column’ to ‘new_column’.&lt;/p&gt;

&lt;p&gt;rails generate migration RenameColumnName&lt;br&gt;
In the generated migration file, you can use the rename_column method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def change
  rename_column :table_name, :old_column, :new_column
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will change the name of the column from ‘old_column’ to ‘new_column’.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Removing Columns&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Scenario 4: Removing a Column&lt;/strong&gt;&lt;br&gt;
If you need to remove a column from a table, Rails migrations can help with that too. Let’s say you want to remove the ‘name’ column from the ‘users’ table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate migration RemoveNameFromUsers name:string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The generated migration file will include the code to remove the ‘name’ column.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Managing Relationships&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Scenario 5: Adding a Foreign Key&lt;/strong&gt;&lt;br&gt;
In Rails, managing relationships between tables is crucial. Suppose you have a ‘User’ model that has many ‘Upload’ records, and you want to add a foreign key to the ‘uploads’ table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate migration AddUserToUploads user:references
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also explicitly specify the foreign_key: true option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def change
  add_reference :uploads, :user, foreign_key: true
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This migration adds a ‘user_id’ column to the ‘uploads’ table, creating a foreign key relationship with the ‘users’ table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario 6: Adding a Unique Constraint&lt;/strong&gt;&lt;br&gt;
To enforce uniqueness on a column, you can create a migration like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate migration AddEmailToUsers email:string:uniq
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The generated migration file will add a ‘unique’ index on the ‘email’ column in the ‘users’ table, ensuring that no two users can have the same email address.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating Join Tables&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Scenario 7: Creating a Join Table&lt;/strong&gt;&lt;br&gt;
Join tables are used to represent many-to-many relationships between models. Suppose you have ‘Customer’ and ‘Product’ models, and you want to create a join table to track which products each customer has purchased:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate migration CreateJoinTableCustomerProduct customer product

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The generated migration file will create a join table called ‘customer_product’ and set up the necessary indexes.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
Database migrations are a powerful tool in Ruby on Rails, allowing you to manage your database schema efficiently. Whether you need to add columns, modify data types, remove columns, manage relationships, or create join tables, Rails migrations provide a structured way to make these changes while keeping your application’s data integrity intact.&lt;/p&gt;

&lt;p&gt;By mastering migrations, you’ll be better equipped to handle database changes in your Rails projects, ensuring a smooth and organized development process. So, don’t hesitate to use these migration commands to shape your database schema and make your application even more powerful.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>databasemigra</category>
      <category>ruby</category>
    </item>
    <item>
      <title>What is slug in rails ?</title>
      <dc:creator>Hardik Radadiya</dc:creator>
      <pubDate>Thu, 09 Jan 2020 12:46:48 +0000</pubDate>
      <link>https://dev.to/hardik355/what-is-slug-in-rails-2gd9</link>
      <guid>https://dev.to/hardik355/what-is-slug-in-rails-2gd9</guid>
      <description></description>
      <category>rails</category>
      <category>ruby</category>
      <category>github</category>
    </item>
    <item>
      <title>Rails js load issues use turbolinks gem.</title>
      <dc:creator>Hardik Radadiya</dc:creator>
      <pubDate>Fri, 13 Dec 2019 10:14:37 +0000</pubDate>
      <link>https://dev.to/hardik355/rails-js-load-issues-use-turbolinks-gem-4j31</link>
      <guid>https://dev.to/hardik355/rails-js-load-issues-use-turbolinks-gem-4j31</guid>
      <description></description>
      <category>rails</category>
      <category>javascript</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Active admin print current user.email but single row</title>
      <dc:creator>Hardik Radadiya</dc:creator>
      <pubDate>Wed, 23 Oct 2019 08:36:56 +0000</pubDate>
      <link>https://dev.to/hardik355/active-admin-print-current-user-email-but-single-row-1cpm</link>
      <guid>https://dev.to/hardik355/active-admin-print-current-user-email-but-single-row-1cpm</guid>
      <description>&lt;p&gt;form title: 'Edit Leave Application' do |f|&lt;br&gt;
    inputs 'LeaveApplication' do &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  f.li do
    f.label :user_id
    f.span f.object.user.email
  end

  f.input :title
  f.input :description
  f.input :status
  f.input :leave_credit
  f.input :leave_type
  f.input :half_leave_session
end
actions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;end&lt;br&gt;
end&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
    </item>
    <item>
      <title>DropboxAuthError: Error getting request token. Is your app key and secret correctly set? Server returned 400: Bad Request</title>
      <dc:creator>Hardik Radadiya</dc:creator>
      <pubDate>Thu, 10 Oct 2019 07:21:24 +0000</pubDate>
      <link>https://dev.to/hardik355/dropboxautherror-error-getting-request-token-is-your-app-key-and-secret-correctly-set-server-returned-400-bad-request-3bi4</link>
      <guid>https://dev.to/hardik355/dropboxautherror-error-getting-request-token-is-your-app-key-and-secret-correctly-set-server-returned-400-bad-request-3bi4</guid>
      <description>&lt;p&gt;rake aborted! DropboxAuthError: Error getting request token. Is your app key and secret correctly set? Server returned 400: Bad Request.&lt;/p&gt;

&lt;p&gt;/home/test/.rvm/gems/ruby-2.3.1/gems/dropbox-sdk-1.6.5/lib/dropbox_sdk.rb:277:in get_token'&lt;/p&gt;

&lt;p&gt;/home/test/.rvm/gems/ruby-2.3.1/gems/dropbox-sdk-1.6.5/lib/dropbox_sdk.rb:293:inget_request_token'&lt;/p&gt;

&lt;p&gt;/home/tesr/.rvm/gems/ruby-2.3.1/gems/dropbox-sdk-1.6.5/lib/dropbox_sdk.rb:299:in get_authorize_url'&lt;/p&gt;

&lt;p&gt;/home/test/.rvm/gems/ruby-2.3.1/gems/paperclip-dropbox-1.3.2/lib/paperclip/dropbox/rake.rb:11:inauthorize'&lt;/p&gt;

&lt;p&gt;/home/test/.rvm/gems/ruby-2.3.1/gems/paperclip-dropbox-1.3.2/lib/paperclip/dropbox/tasks.rake:11:in block (2 levels) in '&lt;/p&gt;

&lt;p&gt;/home/test/.rvm/gems/ruby-2.3.1/gems/rake-12.3.3/exe/rake:27:in'&lt;/p&gt;

&lt;p&gt;/home/test/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in eval'&lt;/p&gt;

&lt;p&gt;/home/test/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in'&lt;/p&gt;

&lt;p&gt;Tasks: TOP =&amp;gt; dropbox:authorize&lt;/p&gt;

</description>
      <category>paperclipdropbox</category>
      <category>rails</category>
      <category>carrierwave</category>
    </item>
    <item>
      <title>rake dropbox:authorize APP_KEY=81zjdh4nfctpdq7 APP_SECRET=mxjs2y3bkeppmsy ACCESS_TYPE = app_folder.</title>
      <dc:creator>Hardik Radadiya</dc:creator>
      <pubDate>Wed, 02 Oct 2019 11:16:25 +0000</pubDate>
      <link>https://dev.to/hardik355/rake-dropbox-authorize-appkey-81zjdh4nfctpdq7-appsecret-mxjs2y3bkeppmsy-accesstype-appfolder-43kd</link>
      <guid>https://dev.to/hardik355/rake-dropbox-authorize-appkey-81zjdh4nfctpdq7-appsecret-mxjs2y3bkeppmsy-accesstype-appfolder-43kd</guid>
      <description>&lt;p&gt;rake aborted!&lt;br&gt;
DropboxAuthError: Error getting request token.  Is your app key and secret correctly set?  Server returned 400: Bad Request.&lt;br&gt;
/home/richard/.rvm/gems/ruby-2.3.1/gems/dropbox-sdk-1.6.5/lib/dropbox_sdk.rb:277:in &lt;code&gt;get_token'&lt;br&gt;
/home/richard/.rvm/gems/ruby-2.3.1/gems/dropbox-sdk-1.6.5/lib/dropbox_sdk.rb:293:in&lt;/code&gt;get_request_token'&lt;br&gt;
/home/richard/.rvm/gems/ruby-2.3.1/gems/dropbox-sdk-1.6.5/lib/dropbox_sdk.rb:299:in &lt;code&gt;get_authorize_url'&lt;br&gt;
/home/richard/.rvm/gems/ruby-2.3.1/gems/paperclip-dropbox-1.3.2/lib/paperclip/dropbox/rake.rb:11:in&lt;/code&gt;authorize'&lt;br&gt;
/home/richard/.rvm/gems/ruby-2.3.1/gems/paperclip-dropbox-1.3.2/lib/paperclip/dropbox/tasks.rake:11:in &lt;code&gt;block (2 levels) in &amp;lt;main&amp;gt;'&lt;br&gt;
/home/richard/.rvm/gems/ruby-2.3.1/gems/rake-12.3.3/exe/rake:27:in&lt;/code&gt;'&lt;br&gt;
/home/richard/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in &lt;code&gt;eval'&lt;br&gt;
/home/richard/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in&lt;/code&gt;'&lt;br&gt;
Tasks: TOP =&amp;gt; dropbox:authorize&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
