- Published
- Author
- Ayush SrivastavaSystem Analyst
We can generate a UUID in Rails using
#CU6U0R822
SecureRandom.uuid without needing any gem#CU6U0R822
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.
SecureRandom.uuid without needing any gemActiveSupport::CurrentAttributes in RailsActiveSupport::CurrentAttributes simplifies the process of storing global, thread-safe data like Current.user or Current.account during requests or jobs. It should be limited to top-level globals, such as user and request details, which are needed across all actions.Current between requests, so we don’t need to manually clear it. However, In Active Jobs, we need to manually reset Current after each job to prevent data from leaking between job executions. We achieve this using the after_perform callback.*app/models/current.rb*:class Current < ActiveSupport::CurrentAttributes
attribute :user, :account, :request_id
endapp/jobs/my_job.rb:class MyJob < ApplicationJob
after_perform :clear_current_attributes
def perform(params)
set_current_attributes(params[:user_id])
end
private
def set_current_attributes(user_id)
Current.user = User.find_by(id: user_id)
Current.request_id = SecureRandom.uuid
end
def clear_current_attributes
Current.reset
end
endCurrent for controllers, but for jobs, we must manually reset it after each job to avoid data leakage.let user: [string, number?] = ["Alice"]; // number is optionalfallbackData parameter in useSWR to provide default data for your fetch request. This is super useful when you want to display initial data while waiting for the network request to resolve. The fallback data will be used as the initial value for the data until the fetcher returns the actual data.const {
data,
mutate,
error,
} = useSWR(endpoint, fetcherFunction, {
fallbackData: initialData,
});sudo groupadd docker # Create the Docker group if it doesn't existsudo usermod -aG docker $USER # Adds the current user to the 'docker' group.newgrp docker # Apply the new group membershipdocker run hello-world # Checks if the user can run Docker commands without sudouseFormContext hook is a part of react-hook-form and allows you to access form methods (such as setValue, getValues, etc.) from any component nested inside the FormProvider. This is useful when you want to manage the form state across deeply nested components without passing props down manually.useFormContext:FormProvider: This allows any child component to access the form context via useFormContext.useFormContext: In your component, you can call useFormContext to access setValue, getValues, etc.FormProvider and pass in useForm's returned values.import { useForm, FormProvider } from "react-hook-form";
const FormComponent = () => {
const methods = useForm();
return (
{/* Now any nested component can use useFormContext */}
{/* Submit button or other components */}
);
};ChildComponent or any other component: Use useFormContext to access the form methods like setValue or getValues.import { useFormContext } from "react-hook-form";
const ChildComponent = () => {
const { setValue } = useFormContext(); // useFormContext gives access to all form methods
return (
{/* Dropdown logic */}
);
};SolidQueue::RecurringExecution.all : You can find and manage recurring jobs with this query.SolidQueue::ReadyExecution.all: Use this query to identify jobs that are ready to run but haven’t started yet.SolidQueue::BlockedExecution.all: Find jobs that are blocked and waiting for conditions to be met before execution.SolidQueue::ClaimedExecution.all: Check jobs that have been claimed by workers but are still in progress.SolidQueue::FailedExecution.all: Use this to track jobs that failed during execution.SolidQueue::ScheduledExecution.all: Find jobs that are scheduled for future execution.SolidQueue::Job.where(finished_at: nil): Query to get jobs that are still running or haven’t finished yet.git commit --amend --no-edit command allows you to modify the most recent commit without changing its commit message.--amend flag updates the previous commit with the new changes.--no-edit option keeps the existing commit message unchanged.git push -fƒ: A dynamic function, typically an API route, that runs on the server and is not statically exported.○: A statically generated (SSG) page, built once during the build process.●: A dynamically generated (SSR) page, built at request time.before_add: Invoked before an object is added to the collection.after_add: Invoked after an object is added to the collection.before_remove: Invoked before an object is removed from the collection.after_remove: Invoked after an object is removed from the collection.Showing 14 to 16 of 82 results
Codemancers can bring your vision to life and help you achieve your goals