Skip to content

Instantly share code, notes, and snippets.

@deepakmahakale
deepakmahakale / autopopulate_migrations.md
Last active December 1, 2016 21:08
Autopopulate migrations with proper command

If the migration name is of the form "AddXXXToYYY" or "RemoveXXXFromYYY" and is followed by a list of column names and types then a migration containing the appropriate add_column and remove_column statements will be created.

Creating a table

$ rails g migration CreateUsers email:string:index

db/migrate/20161201203703_create_users.rb

class CreateUsers < ActiveRecord::Migration
class StringValidator
ALLOWED_PARANTHESIS = {
')' => '(',
'}' => '{',
']' => '['
}.freeze
def initialize(value = '')
@text = value
end
  • By default rails will set the current utc time for created_at and rails will display them by converting into the application timeone
  • You can set application time zone with config.time_zone = 'Eastern Time (US & Canada)'
  • You can set application time zone config.active_record.default_timezone = :utc Supports :utc and :local
  • Use Time.current wherever possible
  • This doesn't work as expected
    Message.where(created_at: '19/01/2018'.to_time.beginning_of_day..'19/01/2018'.to_time.end_of_day)
    Message Exists (0.2ms)  SELECT  1 AS one FROM "messages" WHERE ("messages"."created_at" BETWEEN ? AND ?) LIMIT ?  [["created_at", "2018-01-18 18:30:00"], ["created_at", "2018-01-19 18:29:59.999999"], ["LIMIT", 1]]
    Message Load (0.1ms)  SELECT "messages".* FROM "messages" WHERE ("messages"."created_at" BETWEEN ? AND ?)  [["created_at", "2018-01-18 18:30:00"], ["created_at", "2018-01-19 18:29:59.999999"]]

Change elasticsearch-rails & elasticsearch-model version

-gem 'elasticsearch-model', '0.1.8'
-gem 'elasticsearch-rails', '0.1.8'
+gem 'elasticsearch-rails', '~> 5.0.2'
+gem 'elasticsearch-model', '~> 5.0.2'

Bundle

@deepakmahakale
deepakmahakale / facebook-scrape-url.sh
Created July 5, 2018 05:02
scrape links to share on facebook graph api
curl -X POST \
-F "scrape=true" \
-F "id=https://website.com" \
https://graph.facebook.com

Change root password:

sudo passwd

Change user password:

passwd

I18n Cheatsheet

t('my.messages.hello')

# same as 'my.messages.hello'
t(:hello, scope: 'my.messages')
t(:hello, scope: [:my, :messages])
@deepakmahakale
deepakmahakale / Preferences.sublime-settings
Created July 5, 2018 05:12
Sublime settings for developers
{
"auto_indent": true, /* Optional */
"draw_white_space": "all", /* Optional */
"ensure_newline_at_eof_on_save": true, /* Recommended */
"rulers":
[
80
], /* Optional */
"smart_indent": true, /* Optional */
"tab_size": 2, /* Recommended */
@deepakmahakale
deepakmahakale / scoped_associations.md
Created July 5, 2018 05:13
Avoid N + 1 queries for association

Preload scoped associations

I have two models User and Post with following associations:

# app/models/user.rb
class User < ActiveRecord::Base
  has_many :posts
end