Railsでannotateを使うメモ

modelにスキーマ情報が追加される便利なannotateというライブラリを使用するためにGemfileに以下を追記。

gem 'annotate'

その後、bundle installを実行。

現時点で存在するmodelへスキーマ情報を追加するために以下コマンドを実行

$ bundle exec annotate

こんな感じでスキーマ情報が追加されます。

# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime
# updated_at :datetime
#

class User < ActiveRecord::Base
has_many :articles
end

便利ですね!

RailsでRspecを使うメモ

最近まともにRailsを触り始めました。

よく忘れるのでメモ。

テストはRspecを使いたいので、アプリケーションを作成するときは以下コマンドで。
デフォルトのtest-unit関連のファイル生成しません。

rails new hoge --skip-test-unit

その後、Rspecを使うための設定などを。

Gemfileに以下を追記。

group :test do
gem "rspec"
gem "rspec-rails"
end

追記後、bundle installを実行してライブラリを入れたら、
以下コマンドでRspecを入れる。

$ rails g rspec:install

modelやcontroller作成時に自動でRspecのファイルも生成したいので、
「config/application.rb」を修正。

$ vi config/application.rb

config.generators.test_framework = "rspec" #追加

まだ今のところあまりテスト書いてないですが...

CentOS6.4にMySQL5.6.14をインストールした

いつもだいたいyumで5.5くらいを入れているので、
最新を入れてみようと思った。

公式のrpmで入れてみたけど、簡単ですね。

$ sudo yum -y localinstall http://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-shared-compat-5.6.14-1.el6.x86_64.rpm
$ sudo yum -y localinstall http://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-shared-5.6.14-1.el6.x86_64.rpm
$ sudo yum -y localinstall http://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-server-5.6.14-1.el6.x86_64.rpm
$ sudo yum -y localinstall http://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-devel-5.6.14-1.el6.x86_64.rpm
$ sudo yum -y localinstall http://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-client-5.6.14-1.el6.x86_64.rpm

古いバージョンだとインストール直後は、パスワードなしとかでログインできるけど、
5.6は初期状態でパスワードが設定させていて、以下にパスワードが書かれたファイルが置かれているようですね。

# cat /root/.mysql_secret
# The random password set for the root user at Tue Nov 26 01:54:41 2013 (local time): password

CentOS6.2にMySQLをインストールした

半分寝ながら書いています。

これまた、さくらVPSでのお話し。

事前に必要なモノを入れておく。

# yum install cmake
# yum install ncurses-devel

MySQL用ユーザ作成。

# groupadd mysql
# useradd mysql -g mysql -s /sbin/nologin

インストール。

# cd /usr/local/src/
# wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.24.tar.gz/from/http://cdn.mysql.com/
# tar zxvf mysql-5.5.24.tar.gz
# cd mysql-5.5.24
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
# make
# make install

DBを初期化。

# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
# chown -R mysql:mysql /usr/local/mysql

設定ファイルをコピーして、InnoDBでテーブル毎にデータファイルを作成するように設定。

# cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf

# vi /etc/my.cnf
[mysqld]
innodb_file_per_table

起動スクリプトを用意して、スタート!

# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on
# service mysqld start

CentOS6.2にNginxをインストールした

さくらのVPSでのお話し。

作業メモがてら。

事前に必要なモノを入れておく。

# yum install pcre-devel
# yum install zlib-devel
# yum install openssl-devel

Nginx用ユーザを作成。

# groupadd nginx
# useradd nginx -g nginx -s /sbin/nologin

インストール。

# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.3.2.tar.gz
# tar zxvf nginx-1.3.2.tar.gz
# cd nginx-1.3.2
# ./configure --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module
# make
# make install

起動用スクリプト。

# vi /etc/init.d/nginx

#!/bin/sh                                                                                                                                                                  
#                                                                                                                                                                          
# nginx - this script starts and stops the nginx daemon                                                                                                                    
#                                                                                                                                                                          
# chkconfig:   - 85 15                                                                                                                                                     
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \                                                                                                              
#               proxy and IMAP/POP3 proxy server                                                                                                                           
# processname: nginx                                                                                                                                                       

# Source function library.                                                                                                                                                 
. /etc/rc.d/init.d/functions

# Source networking configuration.                                                                                                                                         
. /etc/sysconfig/network

# Check that networking is up.                                                                                                                                             
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/usr/local/nginx/logs/nginx.lock

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
    ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

実行権限を付与して、自動起動の設定をして、スタート!

# chmod +x /etc/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on
# service nginx start

iPad買った。

新しいiPad買いました。

64GBのWi-Fiモデル(ブラック)にしました。

f:id:shinya__y:20120522205859j:plain

最初は量販店で買おうと思っていましたが、
ポイントがつかないようで、
特に量販店で買うメリットがないようなので、
結局Apple Storeで買いました。

f:id:shinya__y:20120522205434j:plain

外でバリバリ使う感じにはならないと思ったので、
Wi-Fiモデルにしました。

f:id:shinya__y:20120522205348j:plain

おそらく、メインは動画再生になると思います。

f:id:shinya__y:20120522205357j:plain

あとは、アプリの動作チェック用に使ったりするくらいですかね。

CentOS5.7にRubyを入れる

libyaml-develが必要らしいので入れる。
CentOS標準レポジトリにはないのでEPELから入れる。

# cd /usr/local/src
# wget http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
# rpm -Uvh epel-release-5-4.noarch.rpm
# yum --enablerepo=epel install libyaml-devel.x86_64

SQLiteが必要らしいので入れる。
yumで入るバージョンが古いので、ソースから入れる。

# cd /usr/local/src 
# wget http://www.sqlite.org/sqlite-autoconf-3070400.tar.gz
# tar zxvf sqlite-autoconf-3070400.tar.gz
# cd sqlite-autoconf-3070400
# ./configure
# make
# make install

そして、Rubyを入れる。

# cd /usr/local/src/
# wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p0.tar.gz
# tar zxvf ruby-1.9.3-p0.tar.gz
# cd ruby-1.9.3-p0
# ./configure --prefix=/usr
# make
# make install

# ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
# gem -v
1.8.11

ついでにrailsも入れよう。

# gem install rails

と思ったら、以下のようなエラーが出たので

ERROR:  Loading command: install (LoadError)
    cannot load such file -- zlib
ERROR:  While executing gem ... (NameError)
    uninitialized constant Gem::Commands::InstallCommand

zlibを入れてあげる。

# cd /usr/local/src/ruby-1.9.3-p0/ext/zlib
# ruby extconf.rb
# make
# make install

再度railsを入れる。

# gem install rails
file 'lib' not found

# rails -v
Rails 3.2.0

インストールは出来たけど、何かエラー出たので、以下のようにした。

# gem install rails --no-ri --no-rdoc
Successfully installed rails-3.2.0
1 gem installed

# gem install rails
Fetching: rails-3.2.0.gem (100%)
Successfully installed rails-3.2.0
1 gem installed
Installing ri documentation for rails-3.2.0...
Installing RDoc documentation for rails-3.2.0...

とりあえず、大丈夫かな。

新規アプリケーションを作ろうとしたら、エラーが出たので、必要なものを適宜入れる。

# rails new hoge
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- openssl (LoadError)

# yum install openssl-devel.x86_64
# cd /usr/local/src/ruby-1.9.3-p0/ext/openssl/
# ruby extconf.rb
# make
# make install

サーバ起動しようとしたら、エラー出たので対応する。

undefined symbol: sqlite3_initialize

# gem install sqlite3

# vi ~/.bashrc
LD_LIBRARY_PATH=/usr/local/lib
export LD_LIBRARY_PATH
# source ~/.bashrc

またまた何かエラー出たので対応する。

Could not find a JavaScript runtime.

# vi Gemfile
gem 'execjs'
gem 'therubyracer' 

# gem install therubyracer
# bundle install

とりあえず、サーバ起動出来た。。。

# rails s
=> Booting WEBrick
=> Rails 3.2.0 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-01-23 00:34:20] INFO  WEBrick 1.3.1
[2012-01-23 00:34:20] INFO  ruby 1.9.3 (2011-10-30) [x86_64-linux]
[2012-01-23 00:34:20] INFO  WEBrick::HTTPServer#start: pid=11548 port=3000

apacheと連携させるためにPassengerを入れる。

# gem install passenger
# passenger-install-apache2-module

不足しているものが表示されるので、適宜入れていく。
自分の場合は以下を入れました。

# yum install httpd-devel.x86_64
# yum install curl-devel.x86_64

apacheの設定ファイルに追加

LoadModule passenger_module /usr/lib/ruby/gems/1.9.1/gems/passenger-3.0.11/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.9.1/gems/passenger-3.0.11
PassengerRuby /usr/bin/ruby

<VirtualHost *:80>
      ServerName www.yourhost.com
      DocumentRoot /somewhere/public    # <-- be sure to point to 'public'!
      <Directory /somewhere/public>
         AllowOverride all              # <-- relax Apache security settings
         Options -MultiViews            # <-- MultiViews must be turned off
      </Directory>
</VirtualHost>

PATHを設定します。

# vi /etc/sysconfig/httpd
LD_LIBRARY_PATH=/usr/local/lib
export LD_LIBRARY_PATH

うーん。
まだエラーが出ているんですが、後日時間を見つけてエラーをつぶしていこうと思います。
前回やったときは、もっとスムーズにいった気がするんですがね。。。