#chrome

cmd + 0 in mac :)

If you click the tool icon on the right of the address bar, you can also check the current zoom rate.

'Google Chrome Check Zoom Rate'

#zsh

oh-my-zsh is very handy and I’ve been using it for almost a year.

But I found the auto correct is kind of annoying and barely helped me. So I decided to turn if off!

Disable it globally

Add this unsetopt to your ~/.zshrc

source $ZSH/oh-my-zsh.sh

unsetopt correct_all

Disable it per command base

Say if you just want to disable it for git command, you can setup an alias.

alias git=’nocorrect heroku’

I didn’t test out this one :)

References:

#machinist rspec test

CAUTION It’s specific machinist 1.0-maintenance.

Machinist is an alternative for Fixtures or FactoryGirl.

make_unsaved

If you want to generate an object without saving it to the database, replace make with make_unsaved

reference

So remember to use make_unsaved.

More

If you’re interested, here is the source code.

Under latest version

Well if you’re using latest machinist, which is 2.0 at this point, it’s much simpler and cleaner.

make for generating but not saving an object, make! for saving it to database.

#octopress #zsh

Square brackets in zsh

Square brackets in zsh have special meanings but in MOST circumstances maybe you just have to escape them by putting \(backslash) before them.

Here is a example for generating a new octopress post

rake new_post["Escape square bracket by default in zsh"]
zsh: no matches found: new_post[Escape square bracket by default in zsh]

rake new_post\["Escape square bracket by default in zsh"\]
mkdir -p source/_posts
Creating new post: source/_posts/2012-07-08-escape-square-bracket-by-default-in-zsh.markdown

How to escape by default?

Just setup this alias in your .zshrc.

alias rake='noglob rake'
#rails #script

Overview

If you wanna execute some ruby code in rails environment, say manipulate w/ model or something, you can always try rake task first. But there’re circumstances you don’t want to use it, maybe it’s just a one-time script that you don’t want to save it to git, or whatever.

I’ve found out there’re two simple ways to accomplish it.

require config/environment.rb

# test.rb
require "config/environment"
p User.first

# in terminal
ruby test.rb

use rails runner

Rails Guides about rails runner

runner runs Ruby code in the context of Rails non-interactively. For instance:

rails runner "p User.first"
rails runner some_ruby_script.rb
rails runner -e staging "Model.long_running_method"
rails runner -h

You can also use the alias “r” to invoke the runner: rails r.

I failed to call rails r on Rails 3.0.10, maybe it’s above 3.1+.

#rails

I was wondering why people explicitly specify table name in such a scope definition…

class Answer < ActiveRecord::Base
  scope :recent, order('answers.created_at DESC')
end

Until I got this error today.

Mysql2::Error: Column 'created_at' in order clause is ambiguous: SELECT  COUNT(DISTINCT `answers`.`id`) AS count_id, question_id AS question_id FROM `answers` LEFT OUTER JOIN `questions` ON `questions`.`id` = `answers`.`question_id` WHERE (`answers`.user_id = 87) GROUP BY question_id ORDER BY created_at DESC LIMIT 10

I have a recent scope defined in both Question and Answer model. And when you chain those two models, rails don’t know the recent is associated to which one.

@answers = @user.answers.recent.group(:question_id).includes(:question).limit(10)