Thursday, June 2, 2011

Random search in Rails

User.find(:all,'RAND()',:limit =>5)
User.find(:all,'RANDOM()',:limit =>5)

Wednesday, June 1, 2011

Installing Zen Coding eclipse plugin

Select menu
Help > Install New Software ... > and add into location column.

Check Zen Coding for Eclipse and proceed

After instaling finished, type html > Ctrl(command) + E and it will converted <html></html> if installing successfully finished.

Wednesday, May 25, 2011

jQuery Mobile Page transition without Ajax

Disable all
Add $.mobile.ajaxLinksEnabled = false;
*The order loading javascripts have to be jquery > disable code > jquery mobile .
<script type="text/javascript" src=""></script>
<script type="text/javascript">
$(document).bind("mobileinit", function(){
  $.mobile.ajaxLinksEnabled = false;
<script type="text/javascript" src=""></script>

Disable specific link
add rel="external" attribute into a tag.
<a link="#" rel="external">Link Text</a> 

jQuery Mobile How to submit a form without Ajax

Disable all
Add $.mobile.ajaxFormsEnabled = false;
*The order loading javascripts have to be jquery > disable code > jquery mobile .
<script type="text/javascript" src=""></script>
<script type="text/javascript">
$(document).bind("mobileinit", function(){
   $.mobile.ajaxFormsEnabled = false;
<script type="text/javascript" src=""></script>

Disable specific form
add data-ajax="false" attribute into form tag.
<form data-ajax="false">

Friday, May 20, 2011

How to fix Chrome Mac crashes immediately after launch

STEP1: Move to 'Application' folder
STEP2: Right click 'Google Chrome' and choose 'Show Package Contents'
STEP3: Move to 'Contents' folder and delete or move Info.plist and boot Chrome.
And Chrome will launch and fix crash !

If Chrome works fine, restore Info.plist

How to backup MySQL with Rails and backup

'backup' is a RubyGem which allows you to configure and perform backups in a simple manner using an elegant Ruby DSL (for UNIX-like OS: Linux, Mac OSX)

Backup to various strage(Amazon S3,Dropbox and so on) compressed and notice by mail or Twitter.

In this article I'll show you the way to backup database with Rails.

Supported Databases
  • MySQL
  • PostgreSQL
  • MongoDB
  • Redis

Supported Strage
  • Amazon S3
  • Rackspace Cloud Files
  • Dropbox
  • Remote server(FTP SFTP SCP RSync)

Ruby version
  • Ruby 1.9.2
  • Ruby 1.8.7
  • Ruby Enterprise Edition 1.8.7

gem install backup

Crating configure file
backup gem is not only for Rails, so it creates configure file in ~/Backup/config.rb.
It is out of Rails app dir, so generate with '--path' option and create in rails_app_dir/config directory.

Move to Rails app dir and hit the command.
backup generate --path='config/' --databases='mysql' --storages='ftp' --compressors='gzip'
--databases:database type (MySQL、PostgreSQL)
--storages:strage (Amazon S3、FTP)
--compressors:compressor type

And config/config.rb will be generated.

Edit 'config.rb'
Using Backup With Rails
# Backup
# Generated Template
# For more information:
# View the Git repository at
# View the Wiki/Documentation at
# View the issue log at
# When you're finished configuring this configuration file,
# you can run it from the command line by issuing the following command:
# $ backup -t my_backup [-c ]

#Set database name,password refer to 'database.yml'
database_yml = File.expand_path('../config/database.yml',  __FILE__)
RAILS_ENV    = ENV['RAILS_ENV'] || 'development'
require 'yaml'
config = YAML.load_file(database_yml), 'My Backup') do
  database MySQL do |db|               = config[RAILS_ENV]["database"]
    db.username           = config[RAILS_ENV]["username"]
    db.password           = config[RAILS_ENV]["password"]               = "localhost"
    db.port               = 3306
    db.socket             = "/tmp/mysql.sock"
    db.skip_tables        = []
 #Refer to Databases if want to use other database.

 store_with FTP do |server|
   server.username = 'my_username'
   server.password = 'my_password'
   server.ip       = '123.45.678.90'
   server.port     = 21
   server.path     = '~/backups/'
   server.keep     = 5 #number of backups
 #Refer to Storages if want to use other strage.

  compress_with Gzip do |compression| = true = false


configure 'schedule.rb' for whenever gem
whenever is a ruby gem which allow you to set up cron easier.

Backup daily at 5:00 am.
every 1.days, :at=>'05:00' do
command "backup perform --trigger my_backup --config_file 'rails_app/current/config'"
--trigger the name which wrote in 'config.rb', 'My Backup') do ...
--config_file path to 'config.rb'

 After deploy to your production server, 'backup' will automatically backup your database at the time which you set on 'schedule.rb'.

Monday, May 16, 2011

How to create create RSpec helper and share.

Create RSpec helper method and share in all specs if you need.
*RSpec ver 1.3x

Create helper
module UserHelpers
  def user_helper_method
     #do something

describe User
  include UserHelpers
Share in all specs
RSpec.configure do |config|

Thursday, April 7, 2011

How to set Custom Rspec Matchers

rspec version is 1.3.x.
create custom_matchers.rb in application's spec directory, railsapp/spec.
module CustomMatcher
  def matcher_something
    #define custom matcher

edit railsapp/spec/spec_helper.rb
require 'spec/rails'
require File.dirname(__FILE__) + "/custom_matchers"
Spec::Runner.configure do |config|
  config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
And matcher_someting is enabled.

Tuesday, March 8, 2011

How to access Rails app on Mac from VMware Fusion Guest OS.

Virtual Machine > Network > NAT
*NAT is default setting.

On Mac.
System Preferences > Network
Check your IP address.

Then access http://your.ip.address:port_number(http://192.161.1:3000) from your Guest OS.
You may don't need to change your firewall settings.
You must include 'http://' on URL if on IE.

Friday, February 18, 2011

Problem with Rails, created_at or updated_at sometimes becoming null

On my Rails web application, records are sometimes saved with null created_at or updated_at.

This problem occurred for models which have child models.
Therefore I added before_create method in the model.
def before_create
   self.created_at = if self.created_at.blank?
   self.updated_at = if self.updated_at.blank?
This method will fix the problem.