Rails ActiveRecord search records between two dates

Yeah, this is simple in Rails. date = Date.today start_date = date.at_beginning_of_month end_date = date.at_end_of_month Model.where(:created_at => start_date..end_date) This gives the records created in current month. This is simple because rails save created_at and updated_at in following format: created_at: “2018-09-03 09:49:41” But here in my case,  I had to find the records that are valid…

Read more...

Build a beautiful Active Admin interface using Active Admin themes

As we know Active Admin (https://activeadmin.info) is a popular Ruby on Rails plugin to generate admin interface.  It has many benefit over currently existing other similar plugin like Rails Admin. Active Admin gives the facility to customize pages, layouts. Here is simple demo of Active Admin with default theme http://demo.activeadmin.info/admin. Beside this default theme, you…

Read more...

Login to remote Linux server with private key

Here I’m writing how to access Linux server with private key. This is really helpful when you setup remote server first time. For example while creating new instance to the ec2 server, it will allow you to download the private key. You can access to the remote server with this private key. Here is a…

Read more...

PostgreSQL Basic Commands

PostgreSQL PostgreSQL, often simply Postgres, is an object-relational database management system with an emphasis on extensibility and standards compliance. This post is the collection of most commonly used PostgreSQL commands. Login to PostgreSQL psql -Upostgres Where postgres is PostgreSQL username. It will ask password of postgres user. Once you enter password it will enter to…

Read more...

Install and Use Ruby Gems

Ruby gems are the ruby third-party libraries that are created for special purpose. For example, in web application if I want to display large number (>500) of data in a single page, I have to use pagination. For this we have special library build in Ruby which is called Gems. RubyGems is a package manager…

Read more...

Rails tips: ActiveAdmin access filter collection

Since ActiveAdmin uses Ransack for search, we can use Ransack to grab filter collection using their params. This is old method but it works fine to my Rails 5.0.0 and ActiveAdmin 2.0.0 version. ModelName.ransack(params[:q]).result Here is an example controller do def index if params[:q].present? && params[:q][:work_types_in].present? index! do |format| work_types = params[:q][:work_types_in] params[:q].delete(‘work_types_in’) @users =…

Read more...

Run Cron job using whenever gem in production server

The software utility cron is a time-based job scheduler in Unix-like computer operating systems. We can schedule cron job to run specific job in a particular date/time. For ruby we have whenever gem to run those cron job. These are the processes to run whenever gem in remote server: Install the whenever gem Write your…

Read more...

Rails Tips: ActiveRecord Conditional Validation

In my User model, I have to check validation presence of state only for users from Nepal. No need to check validation for the users from other country. For this I have implemented as follows: class User < ActiveRecord::Base validates_presence_of :state, :if => :in_nepal? private def in_nepal? country == ‘Nepal’ end end Here country is…

Read more...

Conference I attended: RubyConfIndia (Ruby Conference India)

RubyConfIndia (http://rubyconfindia.org) is a global event complementing other RubyConf events across the world. This is India edition of ruby conference. This year RubyConfIndia was happened in Bangluru on 9th and 10th Feb 2018. I got chance to attain this event. The event was started with the Keynote talk by Avdi Grimm (@avdi) and closed with…

Read more...

MySQL tips: Back Up and Restore MySQL Database

Sometimes we have to take backup and restore our database. In this post I’m describing how to take backup from MySQL database and restore to Linux/Unix machine. If phpMyAdmin is installed to your system, you can take backup from web portal. If not installed you can take backup from command prompt. Here is MySQL command…

Read more...