Fueling Curiosity, One Insight at a Time

At Codemancers, we believe every day is an opportunity to grow. This section is where our team shares bite-sized discoveries, technical breakthroughs and fascinating nuggets of wisdom we've stumbled upon in our work.

Jul 19, 2024
Resolving gen_random_uuid() Error with PostgreSQL While Implementing CI
When configuring CI with GitHub Actions, I encountered the following error:


PG::UndefinedFunction: ERROR:  function gen_random_uuid() does not exist


This error occurred because the gen_random_uuid() function is not available in PostgreSQL versions older than 11. UUID generation functions were only available through external modules like uuid-ossp and pgcrypto in these older versions.

To resolve this issue, I upgraded to PostgreSQL 13, which includes the gen_random_uuid() function to generate version-4 UUIDs. After upgrading, the error was resolved.



#rails #postgresql #uuid #ci-cd #github-actions
pavankumarreddy
pavankumarreddy
Jul 17, 2024
Jupyter Labs

• To install Jupyter Labs in Mac using Homebrew:


brew install jupyterlab


• To run Jupyter lab:


jupyter lab


This will open JupyterLab in your default web browser.

#python #homebrew
adithya.hebbar
Adithya Hebbar
System Analyst
Jul 16, 2024
The fields_for helper in Rails creates form bindings without rendering a
tag. This is particularly useful for rendering fields for additional model objects within a single form.

Imagine you have a Book model with an associated Author model. You can create a single form for both the Book and Author models using the fields_for helper.



<%= form_with model: @book do |book_form| %>
  <%= book_form.text_field :title %>
  <%= book_form.text_area :description %>

  <%= fields_for :author, @book.author do |author_form| %>
    <%= author_form.text_field :name %>
    <%= author_form.text_field :email %>
  <% end %>
<% end %>


#rails #fields-for #rails-view
giritharan
Giritharan
System Analyst
Jul 16, 2024
rails db:prepare in Ruby on Rails, This command sets up and prepares the database for my application, ensuring everything's ready to go, including populating the database.
#rails #databasesetup
pavankumarreddy
pavankumarreddy
Jul 15, 2024
Handling Terraform State Errors with S3 Backend:
We use an S3 bucket to store our Terraform state. If Terraform fails to update the state, it creates an errored.tfstate file in your working directory. Reapplying will cause errors because the resources already exist.
To fix this, push the errored state back to S3:
terraform state push errored.tfstate

#devops #terraformstateS3 #errorhandling
soniya.rayabagi
soniya.rayabagi
Jul 12, 2024
In Ruby, exception handling is done using begin, rescue, ensure, and end blocks. Here's a brief overview of how they work in a general Ruby context:



begin
  # Code that might raise an exception
rescue SomeExceptionClass => e
  # Code that handles the exception
ensure
  # Code that will always run, regardless of whether an exception was raised
end


begin: Marks the beginning of a block of code that might raise exceptions.

rescue: Specifies what to do if a specific exception is raised. We can rescue multiple exception types by chaining rescue blocks.

ensure: An optional block that will always execute, regardless of whether an exception was raised or rescued. It's useful for cleanup code that must run no matter what.

#ruby #rails
syedsibtain
Syed Sibtain
System Analyst
Jul 12, 2024
Using Ruby's built-in URI::MailTo::EMAIL_REGEXP for email validation is generally better than using a custom regular expression due to its robustness, reliability, and maintenance by the Ruby core team.


class User < ApplicationRecord
  has_secure_password
  validates :name, presence: true

  # Using a custom regular expression for email validation
  validates :email, presence: true, format: { with: /\A[^@\s]+@[^@\s]+\z/ }, uniqueness: true
  
  # Using Ruby's built-in URI::MailTo::EMAIL_REGEXP for email validation
  validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }, uniqueness: true

end


#ruby #regex #rails
syedsibtain
Syed Sibtain
System Analyst
Jul 12, 2024
Fixing Image Rendering in Rails with Active Storage
How to fix the error PG::UndefinedTable: ERROR: relation "active_storage_attachments" does not exist

This error occurs because Active Storage in Rails relies on specific database tables (e.g., active_storage_attachments and active_storage_blobs) to store metadata about attached files. If these tables do not exist, Rails cannot store or retrieve the necessary metadata for file attachments, resulting in the mentioned error.

By following these steps, we ensure that the necessary Active Storage tables are created, allowing Rails to store and retrieve image metadata correctly.

Run Active Storage Installation:


rails active_storage:install


Migrate the Database:


rails db:migrate


Restart the Rails Server:


rails server


Then in views we can simply use the helper and render the image


<%= image_tag url_for(recipe.image), class: "w-full h-64 object-cover" %>


#rails #activestorage
syedsibtain
Syed Sibtain
System Analyst
Jul 10, 2024
Terraform Import :
terraform import allows you to bring existing resources into Terraform's state management without recreating them.
Syntax: terraform import <RESOURCE_TYPE>.<RESOURCE_NAME> <RESOURCE_ID>
Example: terraform import aws_s3_bucket.bucket my-existing-bucket

#devops #TerraformImport
soniya.rayabagi
soniya.rayabagi
Jul 10, 2024
To create a dump of all the inserts with data and column names using pg_dump


pg_dump -U your_username -d your_database -h your_host -p your_port --column-inserts --data-only -f output.sql


#postgres #database
adithya.hebbar
Adithya Hebbar
System Analyst

Showing 14 to 16 of 77 results

Ready to Build Something Amazing?

Codemancers can bring your vision to life and help you achieve your goals