Skip to content

Instantly share code, notes, and snippets.

Avatar
🍹
Surviving The Pandemic

Deepak Mahakale deepakmahakale

🍹
Surviving The Pandemic
View GitHub Profile
@deepakmahakale
deepakmahakale / links.md
Last active Jul 6, 2020
List of sites / tools I normally use
View links.md
@deepakmahakale
deepakmahakale / crontab.sh
Created Dec 2, 2019
A script to restart a node js app and serve over localtunnel (including crontab)
View crontab.sh
# m h dom mon dow command
* * * * * /home/deepak/workspace/status_check.sh >> /home/deepak/workspace/logs/status.log 2>&1
@deepakmahakale
deepakmahakale / speech_ai.js
Created Aug 21, 2019
A twilio function used for NLU
View speech_ai.js
/**
* Hindi Voice Bot
*
* This function lets you ask questions in Hindi and get a proper response
*/
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse()
if(event.SpeechResult) {
var got = require('got');
var requestPayload = {
View using-association-scope.rb
# app/models/user.rb
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
has_many :published_posts, -> { published }, class_name: 'Post'
end
# app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :user
scope :published, -> { where(published: true) }
View N-plus-1-fixed-with-scope.rb
User.includes(:published_posts).map do |user|
[user.name, user.published_posts.map(&:title).join(', ')]
end
User Load (0.3ms) SELECT "users".* FROM "users"
Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE (published = 't') AND "posts"."user_id" IN (1, 2, 3)
#=> [["Jack", "post-1, post-2"], ["Adam", "post-3, post-3"], ["John", "post-5, post-6"]]
View models-with-scoped-associations.rb
# app/models/user.rb
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
has_many :published_posts, -> { where(published: true) }, class_name: 'Post'
end
# app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :user
scope :published, -> { where(published: true) }
View N-plus-1-after-addiding-scope.rb
User.includes(:posts).map do |user|
[user.name, user.posts.published.map(&:title).join(', ')]
end
User Load (0.2ms) SELECT "users".* FROM "users"
Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" IN (1, 2, 3)
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = ? AND "posts"."published" = 't' [["user_id", 1]]
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = ? AND "posts"."published" = 't' [["user_id", 2]]
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = ? AND "posts"."published" = 't' [["user_id", 3]]
#=> [["Jack", "post-1, post-2"], ["Adam", "post-3, post-3"], ["John", "post-5, post-6"]]
View N-plus-1-fixed.rb
User.includes(:posts).map do |user|
[user.name, user.posts.map(&:title).join(', ')]
end
User Load (0.2ms) SELECT "users".* FROM "users"
Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" IN (1, 2, 3)
#=> [["Jack", "post-1, post-2"], ["Adam", "post-3, post-3"], ["John", "post-5, post-6"]]
View N-plus-1-queries-log-1.rb
User.all.map do |user|
[user.name, user.posts.map(&:title).join(', ')]
end
User Load (0.3ms) SELECT "users".* FROM "users"
Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 1]]
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 2]]
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 3]]
#=> [["Jack", "post-1, post-2"], ["Adam", "post-3, post-3"], ["John", "post-5, post-6"]]
View models_before_include.rb
# app/models/user.rb
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
end
# app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :user
scope :published, -> { where(published: true) }
end