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.

Mar 13, 2025
each_with_object is an enumerable method in Ruby that allows you to iterate over a collection while building up an object (like an array or hash). Unlike map, which creates a new array, each_with_object lets you modify an existing object in a single pass.

Syntax


collection.each_with_object(initial_object) do |item, object|
  # Modify the object inside the block
end


collection: The array or enumerable you're iterating over.
initial_object: The object that will be modified (e.g., {} for a hash or [] for an array).
item: The current element in the iteration.
object: The object that accumulates the results.
Example Usage
Using each_with_object with a Hash


numbers = [1, 2, 3, 4, 5]
squares = numbers.each_with_object({}) do |num, hash|
  hash[num] = num**2
end

puts squares
# Output: {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}


Why use each_with_object?
• Avoids the need to initialize an empty {} before the loop.
• Eliminates the need to return the object explicitly.

#CU6U0R822 #ruby
nived.hari
Nived Hari
System Analyst
Mar 13, 2025
When to use collection_select over select in rails
Use collection_select when you need to populate a dropdown with a collection of ActiveRecord objects. It is built on top of select and provides a convenient way to display object attributes instead of a simple array of strings.
select is used for manually defining options, typically from an array of strings or key-value pairs.
collection_select is specifically designed for selecting records from an ActiveRecord collection, making it useful when working with database associations.
mohammad.hussain
Mohammad hussain
System Analyst
Mar 12, 2025
Rake tasks in Rails let you run custom scripts from the command line.
You can define your own tasks inside the lib/tasks directory.

How to Create a Custom Rake Task

1. Create a new .rake file in lib/tasks/


touch lib/tasks/custom_tasks.rake


Define the task inside the file:


namespace :custom do
  desc "Say hello from a custom rake task"
  task :hello do
    puts "Hello from custom Rake task!"
  end
end


Run the task from the terminal:


bin/rake custom:hello


Use :environment if your task interacts with the database or models

#CU6U0R822 #rake
nived.hari
Nived Hari
System Analyst
Mar 12, 2025
The inverse_of option in ActiveRecord helps Rails recognize bidirectional associations in memory, reducing redundant database queries.
For example:


class Employee < ApplicationRecord
  belongs_to :department, foreign_key: 'department_code', primary_key: 'code', inverse_of: :employees
end

class Department < ApplicationRecord
  has_many :employees, foreign_key: 'department_code', primary_key: 'code', inverse_of: :department
end


Why Use inverse_of?
• Prevents extra queries when accessing related objects
• Keeps objects in memory, improving performance
• Ensures associated objects reference the same instance
Without inverse_of, Rails may reload the association unnecessarily:



employee = Employee.first
department = employee.department  # Triggers a SQL query
department.employees.include?(employee)  # Without `inverse_of`, this could trigger another query


With inverse_of, Rails avoids the extra query because it knows department.employees already includes employee

#CU6U0R822 #active_record
nived.hari
Nived Hari
System Analyst
Mar 7, 2025
In dry-validation contracts, values is a hash containing all the parameters being validated. When defining rule blocks, you can access specific parameters using hash-like syntax.
Example:


class MyContract < Dry::Validation::Contract
  params do
    required(:category).filled(:string)
  end

  rule(:category) do
    key.failure("is not allowed") unless values[:category] == "approved_value"
  end
end


Key Points:
values holds all input parameters.
• Use values[:key] to access specific parameters inside rule blocks.
• This allows custom validation logic beyond basic schema definitions.
#ruby #dry_validation
nived.hari
Nived Hari
System Analyst
Mar 7, 2025
You can manually send messages to a Kafka topic using Karafka's producer. This is useful for debugging, testing, or custom event handling.
Example:


payload = {
  id: 123,
  name: "Sample Item",
  status: "processed",
  timestamp: 
Time.now.to_i
}

Karafka.producer.produce_sync(
  topic: "your_topic_name",
  payload: payload.to_json
)


Key Points:
produce_sync ensures the message is sent before proceeding.
topic specifies the Kafka topic where the message will be published.
payload should be serialized into JSON or another supported format.
#karafka
nived.hari
Nived Hari
System Analyst
Mar 6, 2025
Searching in vector databases

1️⃣ Convert Text to Embeddings
• Text is transformed into numerical vectors using AI models like OpenAI, BERT, or Sentence Transformers.
2️⃣ Index & Organise Embeddings
• Instead of scanning all vectors, the database groups similar embeddings into clusters (buckets) to speed up search.
• Common indexing methods:
HNSW (Hierarchical Navigable Small World) – builds a graph where similar embeddings are connected, reducing search time.
IVFFLAT (Inverted File Index) – divides embeddings into clusters (buckets) and searches only the most relevant ones.
3️⃣ Search Using Similarity Metrics
• The query is converted into an embedding and compared to stored vectors using:
Cosine Similarity: Cosine Similarity measures the angle between vectors while ignoring their magnitude, where a higher value means greater similarity (1 = identical, 0 = unrelated, -1 = opposite). It is commonly used for text similarity, such as document searches.
Euclidean Distance: Euclidean Distance calculates the straight-line distance between points, where a lower value means greater similarity (0 = identical). This method is ideal for spatial data, like image or geographical searches.
• The database searches only the closest clusters, making it faster.
4️⃣ Return the Closest Matches
• The best matches (top K documents) are ranked and returned based on similarity scores.
📌 Convert text → embeddings, group them into clusters, search only relevant ones, return the top K ranked results.

#vectordatabase
nitturu.baba
Nitturu Baba
System Analyst
Mar 5, 2025
RAG has three key steps:
1️⃣ Retrieval – Fetch relevant context from a vector database.
2️⃣ Augmentation – Inject the retrieved context into the prompt.
3️⃣ Generation – Use an LLM (GPT, Llama, etc.) to produce a fact-based response.

🔹 Step 1: Retrieval – Finding Relevant Information
Before answering a question, the system searches for relevant documents in a vector database.
💬 Example Question: "What is the capital of France?"
🔍 Retrieval Process:
• The system searches for relevant text in a vector database.
• It finds a stored Wikipedia snippet:
Paris is the capital of France, known for the Eiffel Tower.

📌 Retrieved Context:
Paris is the capital of France, known for the Eiffel Tower.

🔹 Step 2: Augmentation – Enriching the Prompt with Context
After retrieving relevant information, the system adds it to the prompt.

📌 Final Augmented Prompt:
User Question: "What is the capital of France?"
Retrieved Context: "Paris is the capital of France, known for the Eiffel Tower."
Final Prompt: "Using the provided context, answer: What is the capital of France?"

👉 Why is this useful?
Retrieval ensures AI has up-to-date context instead of relying only on pre-trained data.
Augmentation refines the LLM’s input, making answers more precise.
Reduces hallucinations, ensuring the AI doesn’t generate incorrect facts.

🔹 Step 3: Generation – Producing the Final Answer
Once the AI has retrieved and augmented the prompt, it generates a final response.
💡 Example Output:
"The capital of France is Paris, known for the Eiffel Tower and rich history."

#AI #RAG
nitturu.baba
Nitturu Baba
System Analyst
Mar 4, 2025
Updating Session in NextAuth

In NextAuth, you can update the session data using the update function from useSession(). Here's how you can modify user details dynamically:


const { data: session, update } = useSession();

await update({
  user: {
    ...session?.user,
    name: "Updated Name",
    role: "editor", 
  },
});


Assuming a strategy: "jwt" is used, the update() method will trigger a jwt callback with the trigger: "update" option. You can use this to update the session object on the server.



export default NextAuth({
  callbacks: {
    // Using the `...rest` parameter to be able to narrow down the type based on `trigger`
    jwt({ token, trigger, session }) {
      if (trigger === "update" && session?.name) {
        // Note, that `session` can be any arbitrary object, remember to validate it!
        token.name = session.name
        token.role = session.role
      }
      return token
    }
  }
})


This updates the session without requiring a full reload, ensuring the UI reflects the changes immediately. 🚀

#next-auth #nextjs
adithya.hebbar
Adithya Hebbar
System Analyst
Feb 25, 2025
Traits in FactoryBot helps to define reusable variations of a factory without creating multiple factories. They are useful when we need optional attributes or specific states in test data.
Let's say we have a User model with different roles (admin, regular, guest). Instead of writing separate factories, we can use traits like below:


# spec/factories/users.rb
FactoryBot.define do
  factory :user do
    first_name { Faker::Name.first_name }
    email { Faker::Internet.unique.email }
    password { "password123" }

    trait :admin do
      role { "admin" }
    end

    trait :guest do
      role { "guest" }
    end

    trait :confirmed do
      confirmed_at { Time.current }
    end
  end
end


And use it like below


let(:admin_user) { create(:user, :admin) }
let(:guest_user) { create(:user, :guest) }
let(:confirmed_user) { create(:user, :confirmed) }


#CU6U0R822 #factory_bot
puneeth.kumar
Puneeth kumar
System Analyst

Showing 2 to 5 of 76 results

Ready to Build Something Amazing?

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