Skip to content

Instantly share code, notes, and snippets.

@yyandrew
Last active September 30, 2015 04:04
Show Gist options
  • Select an option

  • Save yyandrew/21cf2de3b960d80269b0 to your computer and use it in GitHub Desktop.

Select an option

Save yyandrew/21cf2de3b960d80269b0 to your computer and use it in GitHub Desktop.
SECTION1 - Rails
1.Enumerable inject用法:User.limit(2).inject({}) {|h, u| h[u.id]=u.email;h} #return {1=>"test1@test.com", 2=>"test2@test.com"};
2.查看所有precompile的assets文件 y Rails.application.config.assets.paths
3..gitignore没有作用的解决方法
git rm . -r --cached
git add .
git commit -m "fixed untracked files"
4.install fish shell on ubuntu
#install fish shell
sudo apt-add-repository ppa:fish-shell/release-2
sudo apt-get update
sudo apt-get install fish
# set fish shell as default shell
chsh -s /usr/bin/fish
5.ActiveRecord::StatementInvalid: PG::Error: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list的解决方法
原代码:Company.joins(:votes).where("companies.is_hidden is false").order("votes.updated_at DESC").uniq
修改过的代码:Company.joins(:votes).select("companies.*, votes.updated_at").where("companies.is_hidden is false").uniq.order("votes.updated_at DESC")
6.比group_by更有效率的方法。旧方法:Tag.all.group_by(&:tag_type).keys.新的方法:Tag.select(:tag_type).distinct.map(&:tag_type)。测试1500+个tag。group_by花费大约6ms,然而select只需要1ms.
7.controller的命名空间问题.
1).生成controller: rails g controller admin/tags
class Admin::TagsController < ApplicationController
def show
@tag = Tag.find(params[:id])
end
end
2).routes相关
A).http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Scoping.html;
B).match "/tags/(:id)" => "admin/tags#show", as: :show_tag, via: [:get] # 生成"/tags/test/show",不会包含admin的前缀
8.has_and_belongs_to_many中正确删除方法:@user.tags.delete(Tag.find(tag_params[:id])。这样就不会删除tag而只会删除users_tags中对应的record
9.去除数组中的空内容,nil元素。[12, "12", "", nil].reject(&:blank?) # => [12, "12"]
10.ckedit自定义toolbar的图标
inline javascript code of view
CKEDITOR.replaceAll(function(ta, config){
if(ta.className.indexOf("custom-ckeditor") >= 0) {
config.customConfig = "/javascripts/custom_ckeditor_config.js";
} else if(ta.className.indexOf("custom-survey-ckeditor") >= 0) {
config.customConfig = "/javascripts/custom_survey_ckeditor_config.js";
}
})
/javascripts/custom_ckeditor_config.js
CKEDITOR.editorConfig = function( config ) {
config.toolbar = [
{ name: 'editing', groups: [ 'find', 'selection', 'spellchecker' ], items: [ 'Find', 'Replace', '-', 'SelectAll', '-', 'Scayt' ] }
];
config.language = 'en';
}
11.nest models includes
Tag.includes(:articles).includes(custom_pages: {sub_menu_items: :menu_item}).references(:custom_pages).where("menu_items.id = ?", @section.id)
SECTION2 - System
1.生成8位随机密码: openssl rand -base64 8
2.在Mac OSX上根据端口号查询进程: lsof -n -i4TCP:8090 | grep LISTEN
3.Benchmark of ubunut
1) wget https://freevps.us/downloads/bench.sh -O - -o /dev/null|bash #主要测试下载速度
2) https://github.com/kdlucas/byte-unixbench.git #更加详细的信息
4.ln的使用方法
1).创建 -> ln -s {/path/to/file-name} {link-name}
2).更新 -> ln -sfn {/path/to/file-name} {link-name}
3).删除 -> rm {link-name}
5.删除未知的ssh登录的用户
ps aux | egrep "sshd: [a-zA-Z]+@"
kill [-9] PID
6.rsync同步文件
rsync -r --partial --progress --rsh=ssh REMOTE_IP:SOURCE_DIR DIST_DIR
SECTION3 - Database(Postgresql)
1.将一个数据库中的表的数据导入到另一个数据库: pg_dump -t tablename dbname | psql otherdbname
2.进入到postgres数据库的console: psql dbname username(\d 查看所有表,\d tablename 查看表结构)
3.查看表articles_tags中的index的方法: \d articles_tags;
4.删除表custom_pages中的某个字段sub_menu_item_id
ALTER TABLE custom_pages DROP COLUMN sub_menu_item_id;
SECTION4 - Server
1.502 bad gateway 分别查看: /production_dir/log/thin.log /var/log/monit.log
SECTION5 - VIM
1.Movement
Ctrl-D move half-page down
Ctrl-U move half-page up
Ctrl-B page up
Ctrl-F page down
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment