OraRails

Oracle(9i,10g)+RubyOnRails(Ver1.2.1+Apache+mongrel)の設定メモ

Oracle instantclient2

2007-07-28 16:31:13 | Debian
Debian etch にInstantClientをTNS_NAMESで読めるようにする。

基本はこれOracleInstantClient

tnsnames.oraから参照するには

/usr/local/instantclient/ に network/admin ディレクトリの作成
network/admin 内部に tnsnames.ora,sqlnet.oraを配置する。

export ORACLE_HOME=/usr/local/instantclient
export TNS_ADMIN=/usr/local/instantclient


これでOK

ruby-debug (breakpointerの代替)

2007-07-25 13:57:59 | Debian
Ruby 1.8.5 から script/breakpointer が使えなくなったらしい。
正確には Rails1.1.6 →1.2.3 にあげてから挙動が変わりました。

参考サイト)あさあさの公開メモ簡易リファレンスを参照
ruby script/breakpointer  すると

Breakpoints are not currently working with Ruby 1.8.5
こんなエラーが!!!

代替として ruby-debug というのがあるらしく、早速インストールします。

rootで


# gem install ruby-debug

Bulk updating Gem source index for: http://gems.rubyforge.org
Select which gem to install for your platform (i486-linux)
1. ruby-debug 0.9.3 (ruby)
2. ruby-debug 0.9.2 (ruby)
3. ruby-debug 0.9.1 (ruby)
4. ruby-debug 0.9 (ruby)
5. ruby-debug 0.8.1 (ruby)
6. ruby-debug 0.8 (ruby)
7. ruby-debug 0.7.5 (ruby)
8. ruby-debug 0.7.5 (mswin32)
9. ruby-debug 0.7.4 (ruby)
10. ruby-debug 0.7.4 (mswin32)
11. ruby-debug 0.7.3 (mswin32)
12. ruby-debug 0.7.3 (ruby)
13. ruby-debug 0.7.2 (mswin32)
14. ruby-debug 0.7.2 (ruby)
15. ruby-debug 0.7.1 (mswin32)
16. ruby-debug 0.7.1 (ruby)
17. ruby-debug 0.7 (mswin32)
18. ruby-debug 0.7 (ruby)
19. ruby-debug 0.6.2 (ruby)
20. ruby-debug 0.6.2 (mswin32)
21. ruby-debug 0.6.1 (mswin32)
22. ruby-debug 0.6.1 (ruby)
23. ruby-debug 0.6 (mswin32)
24. ruby-debug 0.6 (ruby)
25. ruby-debug 0.5.3 (ruby)
26. ruby-debug 0.5.2 (ruby)
27. ruby-debug 0.5.2 (mswin32)
28. ruby-debug 0.5.1 (mswin32)
29. ruby-debug 0.5.1 (ruby)
30. ruby-debug 0.5 (ruby)
31. ruby-debug 0.5 (mswin32)
32. ruby-debug 0.4.5 (mswin32)
33. ruby-debug 0.4.5 (ruby)
34. ruby-debug 0.4.4 (mswin32)
35. ruby-debug 0.4.4 (ruby)
36. ruby-debug 0.4.3 (ruby)
37. ruby-debug 0.4.3 (mswin32)
38. ruby-debug 0.4.2 (ruby)
39. ruby-debug 0.4.2 (mswin32)
40. ruby-debug 0.4.1 (mswin32)
41. ruby-debug 0.4.1 (ruby)
42. ruby-debug 0.4 (ruby)
43. ruby-debug 0.4 (mswin32)
44. ruby-debug 0.3 (ruby)
45. ruby-debug 0.3 (mswin32)
46. ruby-debug 0.2 (mswin32)
47. ruby-debug 0.2 (ruby)
48. ruby-debug 0.1.5 (mswin32)
49. ruby-debug 0.1.5 (ruby)
50. ruby-debug 0.1.4 (ruby)
51. ruby-debug 0.1.3 (ruby)
52. ruby-debug 0.1.2 (ruby)
53. Cancel installation
> 1
Install required dependency ruby-debug-base? [Yn] y
Select which gem to install for your platform (i486-linux)
1. ruby-debug-base 0.9.3 (ruby)
2. ruby-debug-base 0.9.3 (mswin32)
3. Cancel installation
> 1
Building native extensions. This could take a while...
ruby extconf.rb install ruby-debug
creating Makefile

make
gcc -I. -I/usr/lib/ruby/1.8/i486-linux -I/usr/lib/ruby/1.8/i486-linux -I. -fPIC -Wall -g -fno-strict-aliasing -O2 -fPIC -c ruby_debug.c
gcc -shared -rdynamic -Wl,-export-dynamic -L"/usr/lib" -o ruby_debug.so ruby_debug.o -lruby1.8 -lpthread -ldl -lcrypt -lm -lc

make install
/usr/bin/install -c -m 0755 ruby_debug.so /var/lib/gems/1.8/gems/ruby-debug-base-0.9.3/lib

make clean
Successfully installed ruby-debug-0.9.3
Successfully installed ruby-debug-base-0.9.3
Installing ri documentation for ruby-debug-0.9.3...
Installing ri documentation for ruby-debug-base-0.9.3...
Installing RDoc documentation for ruby-debug-0.9.3...
Installing RDoc documentation for ruby-debug-base-0.9.3...

インストール完了

さらに、config/environments/development.rb ファイルの一番最後に
 require 'ruby-debug'

あとは、ブレークポイントを入れたい行に
debugger
を入れればOKです。



Mongrel 自動起動 と NameVirtualHost

2007-07-20 10:14:04 | Debian
Debian etch で railsテスト環境と本番環境を分けて管理する手法を作る。

 本番環境  /var/www http://honban.jp
テスト環境 /var/wwwtest http://test.jp

 この中に、同じ構造のディレクトリーを作成しておく。

 参考サイト)バーチャルホストの例 Apache HTTPサーバー

1. DNSで test.jp を追加する
 2. /etc/apache2/sites-available/mainsite をコピーして testsite を作る
 3. testsite の中の
   # virtualhost test.jp #
 Directory
access.log error.log の保存先を変更する

 4. /etc/apache2/sites-available/testsite を ln -s で作成する
 5. proxy_balancer のポート番号を重複しないように注意する

  これだけで、動きます。

 次に、mongrelの自動起動
参考サイト)バリケンのRuby日記

1) mongrel_cluster を組む
cd /var/www/rails1


  mongrel_rails cluster::configure -e development -p 8000 -N 3 - l /var/www/rails1/log/mongrel.log -c /var/www/rails1


mongrel_cluster.yml に 書かれる

  pidファイルを置く場所、ファイル名を変更する
 /var/run/mongrel_cluster/mongrel.pid
→ /var/wwwtest/rails1 の場合は mongrel_test1.pid
  
  cwd: /var/www/rails1
  log_file: /var/www/rails1/log/mongrel.log
  port: "8000"
  environment: development
  pid_file: /var/run/mongrel_cluster/mongrel.pid
  servers: 3

2) 自動起動を作る
 
1. cp /etc/init.d/mongrel_cluster_rails1 /etc/init.d/mongrel_cluster_test1

2.vi mongrel_cluster_test1

mkdir /etc/mongrel_cluster/rails1

#!/bin/bash
#
# Copyright (c) 2007 Bradley Taylor, bradley@railsmachine.com
#
# mongrel_cluster Startup script for Mongrel clusters.
#
# chkconfig: - 85 15
# description: mongrel_cluster manages multiple Mongrel processes for use
# behind a load balancer.
#
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/var/lib/gems/1.8/bin
CONF_DIR=/etc/mongrel_cluster/test1
PID_DIR=/var/run/mongrel_cluster
USER=mongrel
RETVAL=0
# Gracefully exit if the controller is missing.
which mongrel_cluster_ctl >/dev/null || exit 0

# Go no further if config directory is missing.
[ -d "$CONF_DIR" ] || exit 0

case "$1" in
start)
# Create pid directory
mkdir -p $PID_DIR
chown $USER:$USER $PID_DIR

mongrel_cluster_ctl start -c $CONF_DIR
RETVAL=$?
;;
stop)
mongrel_cluster_ctl stop -c $CONF_DIR
RETVAL=$?
;;
restart)
mongrel_cluster_ctl restart -c $CONF_DIR
RETVAL=$?
;;
status)
mongrel_cluster_ctl status -c $CONF_DIR
RETVAL=$?
;;
*)
echo "Usage: mongrel_cluster {start|stop|restart|status}"
exit 1
;;
esac

exit $RETVAL

3. ln -s /etc/mongrel_cluster/test1/mongrel_cluster.yml /var/wwwtest/rails1/config/mongrel_cluster.yml

4. /etc/init.d/mongrel_cluster_test1 start で動くことを確認

  3) rcに組み込む

   /etc/inittab の 
  # default runlevel を確認すると id:2:initdefault:
 
  cd /etc/rc2.d/
ls -la

lrwxrwxrwx 1 root root 17 2007-07-03 10:27 S91apache2 > ../init.d/apache2
lrwxrwxrwx 1 root root 18 2007-07-02 12:35 S99rc.local -> ../init.d/rc.local
lrwxrwxrwx 1 root root 19 2007-07-02 20:26 S99rmnologin -> ../init.d/rmnologin

S90でリンクを作成する

  ln -s ../init.d/mongrel_cluster_rails1 S90mongrel

再起動すると、自動的にmongrelが起動してその後 Apacheが起動する

Sturct構造体の配列

2007-07-05 09:58:03 | 全般
例えば、collection_select_with_currentに使用したい配列があるとする。

配列を構成するために、Structを使う

参考サイト)Rubyリファレンスマニュアル- Struct

参考サイト2)ML Structクラスの配列

table = Struct.new("Table", :func, :len, :flag);
$cmd = [
%w(AAAA AAAA_LEN AAAA_FLAG),
%w(BBBB BBBB_LEN BBBB_FLAG),
%w(CCCC CCCC_LEN CCCC_FLAG)
].collect{ |x| table.new(*x) }

今回 病院名称の配列を作る

table = Struct.new("Table", :hpid, :hpnm);
$hp = [
%w(A1 aaa),
%w(B1 bbb)
].collect{ |x| table.new(*x) }
 
完成!!