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.
Published
Author
Aditya Vishwakarma
System Analyst
We can't use turbo frame for table rows, because html doesn't allow external tag like turbo frame inside the table.
The workaround this is to just have unique id for element you want to modify and use turbosteam to modify only that element.
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
Code
collection.each_with_object(initial_object) do|item, object| # Modify the object inside the blockend
• 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
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
Published
Author
Mohammad hussain
System Analyst
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.
Published
Author
Nived Hari
System Analyst
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/
Code
touch lib/tasks/custom_tasks.rake
Define the task inside the file:
Code
namespace :custom do desc "Say hello from a custom rake task" task :hello do puts "Hello from custom Rake task!" endend
Run the task from the terminal:
Code
bin/rake custom:hello
Use :environment if your task interacts with the database or models
#CU6U0R822 #rake
Published
Author
Nived Hari
System Analyst
The inverse_of option in ActiveRecord helps Rails recognize bidirectional associations in memory, reducing redundant database queries. For example:
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:
Code
employee =Employee.firstdepartment =employee.department # Triggers a SQL querydepartment.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
Published
Author
Nived Hari
System Analyst
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:
Code
classMyContract < Dry::Validation::Contractparamsdorequired(:category).filled(:string)endrule(:category) dokey.failure("is not allowed") unlessvalues[:category] =="approved_value"endend
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
Showing 8 to 10 of 83 results
Ready to Build Something Amazing?
Codemancers can bring your vision to life and help you achieve your goals