Skip to content

Instantly share code, notes, and snippets.

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"]]
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"]]
# 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
@deepakmahakale
deepakmahakale / active_admin.scss
Created February 17, 2019 17:32
Active admin numeric range / number range filter
form.filter_form .filter_form_field.filter_numeric_range input[type='text'] {
background-position: 100% 3px;
background-repeat: no-repeat;
padding-right: 25px;
width: calc(50% - 3px);
}
form.filter_form .filter_form_field.filter_numeric_range .separator, form.filter_form .filter_form_field.filter_numeric_range input[type='text'] {
display: inline-block;
float: none;
@deepakmahakale
deepakmahakale / commands.md
Last active November 13, 2018 15:37
StatsD + influxDB + telegraf

Install StatsD

Make sure nodejs is installed

git clone https://github.com/etsy/statsd.git
cd statsd
cp exampleConfig.js config.js
subl config.js
node stats.js config.js
@deepakmahakale
deepakmahakale / _order_quantity.html.erb
Last active August 30, 2018 04:49
Spree promotions order quantity rule
<%#- app/views/spree/admin/promotions/rules/_order_quantity.html.erb %>
<div class='panel-body'>
<div class='row no-marginb'>
<div class='form-group col-xs-12 col-md-6 no-marginb'>
<%= select_tag "#{param_prefix}[preferred_operator_min]", options_for_select(Spree::Promotion::Rules::OrderQuantity::OPERATORS_MIN.map{|o| [Spree.t("order_quantity_rule.operators.#{o}"),o]}, promotion_rule.preferred_operator_min), { class: 'select2 select_item_total marginb' } %>
<%= select_tag "#{param_prefix}[preferred_operator_max]", options_for_select(Spree::Promotion::Rules::OrderQuantity::OPERATORS_MAX.map{|o| [Spree.t("order_quantity_rule.operators.#{o}"),o]}, promotion_rule.preferred_operator_max), { class: 'select2 select_item_total' } %>
</div>
<div class='form-group col-xs-12 col-md-6 no-marginb'>
<%= text_field_tag "#{param_prefix}[preferred_quantity_min]", promotion_rule.preferred_quantity_min, class: 'form-control marginb' %>
<%= text_field_tag "#{param_prefix}[preferred_quantity_

Change root password:

sudo passwd

Change user password:

passwd
require 'csv'
input_file = "#{Rails.root}/High School Database CSV.csv"
file_1 = CSV.open("#{Rails.root}/High_School_Database_#{1}.csv", 'w')
file_2 = CSV.open("#{Rails.root}/High_School_Database_#{2}.csv", 'w')
file_3 = CSV.open("#{Rails.root}/High_School_Database_#{3}.csv", 'w')
count = 0
CSV.foreach(input_file) do |row|
if count < 400000
file_1 << row
elsif count < 800000
@deepakmahakale
deepakmahakale / new css and js
Created July 5, 2018 05:17
find new css and js in two branches
git diff --color --name-status master..develop | grep -E '^A.*.js|^A.*.css' # js & css
git diff --color --name-status master..develop | grep -E '^A.*db/migrate/.*.rb' # migrations
@deepakmahakale
deepakmahakale / reversible_migration.md
Created July 5, 2018 05:16
Reversible migration for dropping a table
class DropUsers < ActiveRecord::Migration
  def change
    drop_table :users do |t|
      t.string :email, null: false
      t.timestamps null: false
    end
  end
end