Modules are commonly used to to group constants holding possible attribute values. Like in the example below we have a Order class and it has attribute status.
Status can hold different values as pending, confirmed, cancelled etc.
class Order < ApplicationRecord
PENDING = 'Pending'
CONFIRMED = 'Confirmed'
CANCELLED = 'Cancelled'
DECLINED = 'Declined'
COMPLETED = 'Completed'
validates_inclusion_of :status, in: [Status::PENDING, Status::CONFIRMED, Status::CANCELLED, Status::DECLINED, Status::COMPLETED]
When a new order is placed or existing order updated we want to ensure that status attribute contains valid value. We are using rails validation method validates_inclusion_of to achieve that. …
Sometimes your application get overwhelmed with requests. These requests can block other users from accessing the application. To prevent this you can set a threshold on number of requests a user can make for any endpoint in given span of time.
I am using rack-attack gem to active this.
Add rack-attack gem. Add it in you Gemfile and run bundle install command.
Add rack_attack.rb file in initialisers with code snippet given below. Here we will be adding api rate limit for login api. I am allowing 10 requests per minute and 50 per day. …
After completing my secondary schooling I moved to another city in August 2010. I never got chance to move back to my hometown until 2020 when pandemic began. I came back to my hometown in July 2020, almost after 10 years. I am staying here from last 6 months now. Both of my siblings also moved here during pandemic.
Initially when I came here we used to buy vegetables in every 2–3 days. I remember the struggle with washing those vegetables in clean water, followed by salt water and then keep them aside for some time.
Being from farmers family…
While using ActiveRecord for data operations it’s very important to write optimal queries to avoid multiple sql queries or loading unused data in memory which may impact performance of the application. Lets explore one of the such queries.
Scenario - You have a user table having column email and you want to list all uniq email address from the table. you can achieve this by:
2. Fetch uniq email ids from database with command below:
We all know benefits of logging. It enable developers to quickly troubleshoot issues even before reproducing it. These can be logs generated by framework, custom logs added by developers.
In Production environment we have many app servers running behind the load balancer. Request can be processed by any server and hence to get the request logs you need to know which server has processed the request. It gets even more complex when you have multiple services in place.
We all know benefits of logging. It enable developers to quickly troubleshoot issues even before reproducing it.
In distributed systems, computing logs are stored in shared drive or sometimes on distributed servers itself. Later these logs are aggregated in tools like Splunk, LogDNA, Logstash to make them accessible at single place and enable users to search/read logs.
Since these tool are not managed by your organisation should you be pushing sensitive information likes personal details of your customers, authentication details, payment information on these tools? Obvious answer is NO! So these details are masked in most of the application before…
Here we will be using RingCaptcha authentication api to send OTP from Rails application. With free trial account you will get 100 OTP sms balance. Follow below steps to get started!
Visit RingCaptcha portal and create your free trial account using your email address and phone number. You can also create a new account using your Github account.
A uniq API key will be generated for your account. You can get this API key in My Apps section on home page.
Next you need to create a new app. You can create multiple apps(each for a environment) in the same…
Here we will be using 2FACTOR authentication api to send OTP from Rails application in India. With free trial account you will get 100 OTP sms, 100 Transactional SMS and 50 voice sms. Follow below steps to get started!
Visit 2FACTOR portal and create your free trial account using your email address and phone number. A uniq API key will be generated for your account. You will receive this API key in email inbox. You can also find the same on 2factor portal in your account summery. Check available sms balance there.
Add httparty gem in your Gemfile. Run bundle…
Rspec is commonly used testing framework for writing unit test of Rails/Ruby applications. A unit test is written to test a behaviour of a single method. When it has a conditional branching then, its good to write a separate test for each branching.
To test a method or a brach of code following attributes are required:
1. Data setup
3. Method call
Lets have look at each attributes:
Apache Solr is open source search platform used for enhanced search and indexing.For more details please visit Apache Solr’s official website.
If you are using Oracle database in your application and want to enable Solr search then you can use data importers provided by Solr to read data from your database and create create documents in Solr. You will be able to find this option on Solr UI once you select the core. You need to provide Oracle database details in order to use this functionality.
Lets start with creating a new core and connecting existing database to core and…