Ruby on Rails, Gmail and Net::SMTPAuthenticationError: 535-5.7.8 Username and Password not accepted

Are you getting following error while using Gmail SMTP?

Net::SMTPAuthenticationError: 535-5.7.8 Username and Password not accepted

The solution is simple.

  1. Go to https://www.google.com/settings/security/lesssecureapps
  2. Enable the switch marked below and you are done!

Allow Less Secure Apps

Dotenv : Are the environment variables not loading in the `production` environment of your Rails application?

You have configured your .env for all common environment variables across environments but they are not loading in production environment.

Here is a catch in the intallation doc.

could not fetch specs from https://rubygems.org/

Here you can see that the document mentions to install the gem only in development and test environments and you just copied and pasted as is in your Gemfile !

The fix is simple. Just remove groups: [:development, :test] or put the dotenv-rails gem outside of all environment like ohter gems required in all environment.

CanCanCan : Use differnt column to `load_and_authorize_resource` in inherited controllers

Recently I came across a situation where there was a need to use differnt column for load_and_authorize_resource in an inherited controller.

By default CanCanCan, use id to find resources in load_and_authorize_resource

If you want to use different column, you can use :find_by option:

load_and_authorize_resource :find_by => :slug

But this will apply on all the controller inheriting it.

# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base

  load_and_authorize_resource

end
# app/controllers/posts_controller.rb
class PostsController < ApplicationController

  # for this controller, resource will be load using `id` 

end

If you try to put load_and_authorize_resource :find_by => :slug in posts_controller.rb hoping it will override the behaviour then you are wrong. It doesn’t work.

To make it working, you need to use :prepend option.

# app/controllers/posts_controller.rb
class PostsController < ApplicationController

  load_and_authorize_resource :find_by => :slug, prepend: true

end

Hope this helps!

How to install JetBrains Mono on Ubuntu and enable in Sublime?

JetBrains recently released JetBrains Mono fonts for free.

It is very friendly to developers as the font has,

  • Increased height and letter spacing for a better reading experience(study says we spend 80%+ time on reading code)

without ligatures

  • Code-specific ligatures merges multiple symbols helps eyes to process less

Without Ligature without ligatures

With Ligaturewithout ligatures

  • And it’s free & open source

How install in Ubuntu?

Download and Install

Option 1

Use browser to download zip from Jetbrains Mono page and unzip it in ~/.local/share/fonts/ to use it for specific user or /usr/share/fonts for system wide use.

Option 2

Use command line

$ wget https://download.jetbrains.com/fonts/JetBrainsMono-2.001.zip
$ unzip JetBrainsMono-2.001.zip

To use it for specific user:

$ mv JetBrainsMono-*.ttf ~/.local/share/fonts/

To use it sysem-wide:

$ sudo mv JetBrainsMono-*.ttf /usr/share/fonts/

How to enable in Sublime Text?

Download latest version from here

In sublime,

  1. Open Preferences > Settings
  2. Add "font_face": "JetBrains Mono" on the User’s pref
  3. Save (Ctrl+S)

without ligatures

Thant’s it!

How to fix `DEPRECATION WARNING: Dangerous query method` on `pluck`?

Suddenly I noticed a peculiar deprecation warning on .pluck in log everywhere.

DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s): [:client_id, :duns, :legal_business_name]. Non-attribute arguments will be disallowed in Rails 6.1. This method should not be called with user-provided values, such as request parameters or model attributes. Known-safe values can be passed by wrapping them in Arel.sql()

Here is a simplified snippet which was generating the warning:

ERRORED_RECORDS_FIELDS = %i(client_id duns legal_business_name)
...
...
Client.pluck(ERRORED_RECORDS_FIELDS)

However, the result returns just fine. Reading a bit around the warning I relized that I have been passing array if fields which at first point looks ok but it’s not.

So instead of passing array, splat the array so that all elements are received as separate column value in the args of the pluck method.

So following fixes the issue.

Client.pluck(*ERRORED_RECORDS_FIELDS)

Thanks!