rails Blogs
Written by Kiprosh, team of passionate and disciplined craftsmen turning your ideas into reality.
Kiprosh is now part of LawLytics
Written by Kiprosh, team of passionate and disciplined craftsmen turning your ideas into reality.
In ActiveAdmin, if your resource model is deriving some data from other models, using some kind of relationship(has_one or has_many). In this scenarios where you want to search and filter the resource based on the data in its related table, you can follow below: Suppose you have a model School, and a model Student, now you want to search or filter students based on the school name. We can filter using join(:schools).where("schools.name like") or say an In Query which is inefficient. Either way ActiveAdmin does not provide default filters based on
Feature is used when you need to save your nested model with the parent model. Use-Case: Suppose you have a model say Person and it can be associated with N number of Contacts. Using nested form we can associate 1..n contacts to the person. Approach: For associating a contact to a person, we need to build the contact model with respect to the person model. For has_many relation, @contact = @person.contacts.build and if has_one relation @contact = @person.build_contact. Now we can use the @contact in the view, for this we use fields_for tag. And
In a Ruby On Rails applications where we follow convention over configuration, we might get hit by a slow server response, that would eventually cost users shy away from application. There are many ways of how you can boost performance of Ruby On Rails applications. Focusing on approach depending on the application structure, size of the database and traffic intensity, our performance optimization can be carried out. Rules for Performance Optimization Measure Optimize What is slow Test and Isolate changes Primary areas to think about where we can improve performance are Views and ActiveRecord Queries PS Note : Will be continued.
Just found that I couldn't do zeus c test for a test environment console Here's a workaround or small hack to do this Steps On your console do zeus init This will create two files zeus.json custom_plan.rb Add this snippet to your custom_plan.rb and define the tasks for Zeus. Mine looks like this: require 'zeus/rails' class CustomPlan Edit zeus.json to include only the tasks for which you’ll use Zeus. Mine looks like this: { "command": "ruby -rubygems -r./custom_plan -eZeus.go", "plan": { "boot": { "default_bundle": { "development_environment": { "prerake": {"rake": []}, "runner": ["r"], "console"
How does Unicorn works out? Unicorn works in cluster wherein it queues requests to multiple workers. Whenever the Unicorn master starts, it loads the app into its memory and handles to serve the requests to its workers. Here is a link which will elaborate the architecture for Unicorn in a nutshell: [Unicorn Architecture(https://github.com/blog/517-unicorn)][1] This is one of the good architecture that helps to distribute requests to provide load balancing. At the same time, there might occur an issue with Data Transaction (Concurrency). Would like to give a small scenario that created a issue in
While writing functional test cases, we might need a common utility to be used through our test cases. These utility is generally recognized as "Macros" and more specifically as "Controller Macros". With Rspec we can easily configure such Macros, as Rspec specifically provide a RSpec.configure block to define all our utility and support classes/modules used throughout our test suit. The RSpec.configure block looks like this. RSpec.configure do config.extend(AuthenticateMacro) end With Test::Unit which is a default testing tool provided by rials and its much more liter than Rspec, we can't
Consider a scenario where multiple models has 1 to N relationship with one model. For example. Consider models like School, College, Event and Semester are having multiple relationship with a single model i.e Student. It will not be a good idea to have multiple foreign keys to student model like school_id, college_id, event_id and semester_id into one table. So whenever you have a scenario like this, you have to follow polymorphic associations. For example: In your models i.e school.rb, college.rb, semester.rb and event.rb, define something like this: class School <
ActiveRecord::Base#find_in_batches. This lets you iterate over all the records in cursor-like fashion (only retrieving a set number of records at a time into the memory): @Subscription.find_in_batches(batch_size => 100 ) { |subs| subs.each { |s| ... } }@ iterate over all subscriptions in chunks of 100 find_in_batches supports scopes @class Subscription < ActiveRecord::Base scope :expired, :conditions => { :expired => true } end @ @Subscription.expired.find_in_batches(:batch_size => 100 ) { |subs| ... }@ _Disadvantage: _ You can’t specify :order or :limit in the options. Where to use it: Large Dataset, where you need to loop through