mixi clone on rails 01

Ruby on Rails の勉強してるけど、やっぱり本読んでるだけじゃ身に付かないよね、ってことでなんか適当なネタを Rails で実装してみようと思います。

昔友達と PHP の勉強のために mixi のクローン(要するにパクり)を実装しようとした(そして途中で飽きてやめた)ことがあったけど、同じことを Ruby on Rails でやってみます。

また途中で飽きる可能性大ですけど。

Rails 環境の準備

Mac mini の上の VMware Fusion の上の Debian 4.0 の上に作ります。一通り必要となりそうなものを aptitude でインストール。

まずは rails から。

debian:~# aptitude install rails
Reading package lists... Done
Building dependency tree... Done
Reading extended state information
Initializing package states... Done
Writing extended state information... Done
Reading task descriptions... Done
Building tag database... Done
The following NEW packages will be automatically installed:
irb irb1.8 liberb-ruby libpgsql-ruby1.8 libpq4 libreadline-ruby1.8
libredcloth-ruby1.8 libruby1.8 rake rdoc rdoc1.8 ruby ruby1.8
The following packages have been kept back:
initramfs-tools linux-image-2.6-amd64 linux-image-2.6.18-4-amd64
The following NEW packages will be installed:
irb irb1.8 liberb-ruby libpgsql-ruby1.8 libpq4 libreadline-ruby1.8
libredcloth-ruby1.8 libruby1.8 rails rake rdoc rdoc1.8 ruby ruby1.8
0 packages upgraded, 14 newly installed, 0 to remove and 3 not upgraded.
Need to get 4840kB of archives. After unpacking 23.8MB will be used.
Do you want to continue? [Y/n/?]
Writing extended state information... Done
Get:1 http://ftp.jp.debian.org etch/main libruby1.8 1.8.5-4 [1581kB]
Get:2 http://ftp.jp.debian.org etch/main ruby1.8 1.8.5-4 [218kB]
Get:3 http://ftp.jp.debian.org etch/main libreadline-ruby1.8 1.8.5-4 [197kB]
Get:4 http://ftp.jp.debian.org etch/main irb1.8 1.8.5-4 [235kB]
Get:5 http://ftp.jp.debian.org etch/main irb 1.8.2-1 [3662B]
Get:6 http://ftp.jp.debian.org etch/main liberb-ruby 2.0.4+ruby1.8.2-1 [3530B]
Get:7 http://ftp.jp.debian.org etch/main libpq4 8.1.9-0etch1 [274kB]
Get:8 http://ftp.jp.debian.org etch/main libpgsql-ruby1.8 0.7.1-10 [40.7kB]
Get:9 http://ftp.jp.debian.org etch/main libredcloth-ruby1.8 3.0.99.0.svn.20060519-1 [68.0kB]
Get:10 http://ftp.jp.debian.org etch/main ruby 1.8.2-1 [19.0kB]
Get:11 http://ftp.jp.debian.org etch/main rake 0.7.1-1 [109kB]
Get:12 http://ftp.jp.debian.org etch/main rdoc1.8 1.8.5-4 [309kB]
Get:13 http://ftp.jp.debian.org etch/main rdoc 1.8.2-1 [3856B]
Get:14 http://ftp.jp.debian.org etch/main rails 1.1.6-3 [1777kB]
Fetched 4840kB in 9s (508kB/s)
Selecting previously deselected package libruby1.8.
(Reading database ... 22297 files and directories currently installed.)
Unpacking libruby1.8 (from .../libruby1.8_1.8.5-4_amd64.deb) ...
Selecting previously deselected package ruby1.8.
Unpacking ruby1.8 (from .../ruby1.8_1.8.5-4_amd64.deb) ...
Selecting previously deselected package libreadline-ruby1.8.
Unpacking libreadline-ruby1.8 (from .../libreadline-ruby1.8_1.8.5-4_amd64.deb) ...
Selecting previously deselected package irb1.8.
Unpacking irb1.8 (from .../irb1.8_1.8.5-4_all.deb) ...
Selecting previously deselected package irb.
Unpacking irb (from .../archives/irb_1.8.2-1_all.deb) ...
Selecting previously deselected package liberb-ruby.
Unpacking liberb-ruby (from .../liberb-ruby_2.0.4+ruby1.8.2-1_all.deb) ...
Selecting previously deselected package libpq4.
Unpacking libpq4 (from .../libpq4_8.1.9-0etch1_amd64.deb) ...
Selecting previously deselected package libpgsql-ruby1.8.
Unpacking libpgsql-ruby1.8 (from .../libpgsql-ruby1.8_0.7.1-10_amd64.deb) ...
Selecting previously deselected package libredcloth-ruby1.8.
Unpacking libredcloth-ruby1.8 (from .../libredcloth-ruby1.8_3.0.99.0.svn.20060519-1_all.deb) ...
Selecting previously deselected package ruby.
Unpacking ruby (from .../archives/ruby_1.8.2-1_all.deb) ...
Selecting previously deselected package rake.
Unpacking rake (from .../archives/rake_0.7.1-1_all.deb) ...
Selecting previously deselected package rdoc1.8.
Unpacking rdoc1.8 (from .../rdoc1.8_1.8.5-4_all.deb) ...
Selecting previously deselected package rdoc.
Unpacking rdoc (from .../archives/rdoc_1.8.2-1_all.deb) ...
Selecting previously deselected package rails.
Unpacking rails (from .../archives/rails_1.1.6-3_all.deb) ...
Setting up libruby1.8 (1.8.5-4) ...
Setting up ruby1.8 (1.8.5-4) ...
Setting up libreadline-ruby1.8 (1.8.5-4) ...
Setting up irb1.8 (1.8.5-4) ...
Setting up irb (1.8.2-1) ...
Setting up liberb-ruby (2.0.4+ruby1.8.2-1) ...
Setting up libpq4 (8.1.9-0etch1) ...
Setting up libpgsql-ruby1.8 (0.7.1-10) ...
Setting up libredcloth-ruby1.8 (3.0.99.0.svn.20060519-1) ...
Setting up ruby (1.8.2-1) ...
Setting up rake (0.7.1-1) ...
Setting up rdoc1.8 (1.8.5-4) ...
Setting up rdoc (1.8.2-1) ...
Setting up rails (1.1.6-3) ...

データベースには PostgreSQL 8.1 を使います。 MySQL 慣れてないんで。

debian:~# aptitude install postgresql-8.1
Reading package lists... Done
Building dependency tree... Done
Reading extended state information
Initializing package states... Done
Writing extended state information... Done
Reading task descriptions... Done
Building tag database... Done
The following NEW packages will be automatically installed:
openssl postgresql-client-8.1 postgresql-client-common postgresql-common
ssl-cert
The following packages have been kept back:
initramfs-tools linux-image-2.6-amd64 linux-image-2.6.18-4-amd64
The following NEW packages will be installed:
openssl postgresql-8.1 postgresql-client-8.1 postgresql-client-common
postgresql-common ssl-cert
0 packages upgraded, 6 newly installed, 0 to remove and 3 not upgraded.
Need to get 7037kB of archives. After unpacking 20.7MB will be used.
Do you want to continue? [Y/n/?]
Writing extended state information... Done
Get:1 http://ftp.jp.debian.org etch/main openssl 0.9.8c-4 [1005kB]
Get:2 http://ftp.jp.debian.org etch/main postgresql-client-common 71 [40.2kB]
Get:3 http://ftp.jp.debian.org etch/main postgresql-client-8.1 8.1.9-0etch1 [1454kB]
Get:4 http://ftp.jp.debian.org etch/main ssl-cert 1.0.14 [11.1kB]
Get:5 http://ftp.jp.debian.org etch/main postgresql-common 71 [102kB]
Get:6 http://ftp.jp.debian.org etch/main postgresql-8.1 8.1.9-0etch1 [4424kB]
Fetched 7037kB in 16s (432kB/s)
Preconfiguring packages ...
Selecting previously deselected package openssl.
(Reading database ... 24886 files and directories currently installed.)
Unpacking openssl (from .../openssl_0.9.8c-4_amd64.deb) ...
Creating directory /etc/ssl
Selecting previously deselected package postgresql-client-common.
Unpacking postgresql-client-common (from .../postgresql-client-common_71_all.deb) ...
Selecting previously deselected package postgresql-client-8.1.
Unpacking postgresql-client-8.1 (from .../postgresql-client-8.1_8.1.9-0etch1_amd64.deb) ...
Selecting previously deselected package ssl-cert.
Unpacking ssl-cert (from .../ssl-cert_1.0.14_all.deb) ...
Selecting previously deselected package postgresql-common.
Unpacking postgresql-common (from .../postgresql-common_71_all.deb) ...
Selecting previously deselected package postgresql-8.1.
Unpacking postgresql-8.1 (from .../postgresql-8.1_8.1.9-0etch1_amd64.deb) ...
Setting up openssl (0.9.8c-4) ...
Setting up postgresql-client-common (71) ...
Setting up postgresql-client-8.1 (8.1.9-0etch1) ...
Setting up ssl-cert (1.0.14) ...
Setting up postgresql-common (71) ...
adduser: Warning: that home directory does not belong to the user you are currently creating.
Setting up postgresql-8.1 (8.1.9-0etch1) ...
Creating new cluster (configuration: /etc/postgresql/8.1/main, data: /var/lib/postgresql/8.1/main)...
Moving configuration file /var/lib/postgresql/8.1/main/postgresql.conf to /etc/postgresql/8.1/main...
Moving configuration file /var/lib/postgresql/8.1/main/pg_hba.conf to /etc/postgresql/8.1/main...
Moving configuration file /var/lib/postgresql/8.1/main/pg_ident.conf to /etc/postgresql/8.1/main...
Configuring postgresql.conf to use port 5432...
Starting PostgreSQL 8.1 database server: main.

以上でとりあえず必要となるパッケージのインストールは終わり。

では Rails のプロジェクトを作りましょう。

mixi の clone ということで mixlone というプロジェクト名にします。

一般ユーザで以下のコマンドを実行します。

masakazu@debian:~$ rails mixlone
create
create  app/controllers
create  app/helpers
create  app/models
create  app/views/layouts
create  config/environments
create  components
create  db
create  doc
create  lib
create  lib/tasks
create  log
create  public/images
create  public/javascripts
create  public/stylesheets
create  script/performance
create  script/process
create  test/fixtures
create  test/functional
create  test/integration
create  test/mocks/development
create  test/mocks/test
create  test/unit
create  vendor
create  vendor/plugins
create  tmp/sessions
create  tmp/sockets
create  tmp/cache
create  Rakefile
create  README
create  app/controllers/application.rb
create  app/helpers/application_helper.rb
create  test/test_helper.rb
create  config/database.yml
create  config/routes.rb
create  public/.htaccess
create  config/boot.rb
create  config/environment.rb
create  config/environments/production.rb
create  config/environments/development.rb
create  config/environments/test.rb
create  script/about
create  script/breakpointer
create  script/console
create  script/destroy
create  script/generate
create  script/performance/benchmarker
create  script/performance/profiler
create  script/process/reaper
create  script/process/spawner
create  script/runner
create  script/server
create  script/plugin
create  public/dispatch.rb
create  public/dispatch.cgi
create  public/dispatch.fcgi
create  public/404.html
create  public/500.html
create  public/index.html
create  public/favicon.ico
create  public/robots.txt
create  public/images/rails.png
create  public/javascripts/prototype.js
create  public/javascripts/effects.js
create  public/javascripts/dragdrop.js
create  public/javascripts/controls.js
create  public/javascripts/application.js
create  doc/README_FOR_APP
create  log/server.log
create  log/production.log
create  log/development.log
create  log/test.log

Rails には ActiveRecord という Ruby オブジェクトと RDBMS を結びつけるための仕組みがありますけど、この ActiveRecord のデータベース関連の設定は config/database.yml で行います。デフォルトでは MySQL に接続するような設定になってますけど、今回は PostgreSQL を使いますので、アカウントとパスワードもあわせて以下のように修正します(修正箇所を bold にしてます)。

FILE: config/database.yml
1  # MySQL (default setup).  Versions 4.1 and 5.0 are recommended.
2  #
3  # Install the MySQL driver:
4  #   gem install mysql
5  # On MacOS X:
6  #   gem install mysql -- --include=/usr/local/lib
7  # On Windows:
8  #   There is no gem for Windows.  Install mysql.so from RubyForApache.
9  #   http://rubyforge.org/projects/rubyforapache
10  #
11  # And be sure to use new-style password hashing:
12  #   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
13  development:
14    adapter: postgresql
15    database: mixlone_development
16    username: mixlone
17    password: mixlone
18    host: localhost
19
20  # Warning: The database defined as 'test' will be erased and
21  # re-generated from your development database when you run 'rake'.
22  # Do not set this db to the same as development or production.
23  test:
24    adapter: postgresql
25    database: mixlone_test
26    username: mixlone
27    password: mixlone
28    host: localhost
29
30  production:
31    adapter: postgresql
32    database: mixlone_production
33    username: mixlone
34    password: mixlone
35    host: localhost

この通りにデータベースを準備しましょう。 postgres アカウントになって以下のコマンド実行します。

debian:~# su - postgres
postgres@debian:~$ createuser mixlone
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n
CREATE ROLE
postgres@debian:~$ createdb -O mixlone -E UTF-8 mixlone_development
CREATE DATABASE
postgres@debian:~$ createdb -O mixlone -E UTF-8 mixlone_test
CREATE DATABASE
postgres@debian:~$ createdb -O mixlone -E UTF-8 mixlone_production
CREATE DATABASE
postgres@debian:~$ psql template1
Welcome to psql 8.1.9, the PostgreSQL interactive terminal.
Type:  \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
template1=# alter user mixlone password 'mixlone';
ALTER ROLE
template1=# \q

以上でとりあえずの準備は完了です。

試しにサーバを起動して接続してみましょう。

masakazu@debian:~/mixlone-$ script/server -b 0.0.0.0
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2007-09-04 08:39:27] INFO  WEBrick 1.3.1
[2007-09-04 08:39:27] INFO  ruby 1.8.5 (2006-08-25) [x86_64-linux]
[2007-09-04 08:39:27] INFO  WEBrick::HTTPServer#start: pid=3849 port=3000

適当なブラウザからアクセスするとこんな画面が表示されます。

サーバに接続できることを確認できたら Ctrl+C で抜けましょう。

192.168.1.102 - - [04/Sep/2007:08:49:55 JST] "GET / HTTP/1.1" 304 0
- -> /
192.168.1.102 - - [04/Sep/2007:08:49:55 JST] "GET /javascripts/prototype.js HTTP/1.1" 304 0
http://192.168.1.194:3000/ -> /javascripts/prototype.js
192.168.1.102 - - [04/Sep/2007:08:49:55 JST] "GET /javascripts/effects.js HTTP/1.1" 304 0
http://192.168.1.194:3000/ -> /javascripts/effects.js
192.168.1.102 - - [04/Sep/2007:08:49:55 JST] "GET /images/rails.png HTTP/1.1" 304 0
http://192.168.1.194:3000/ -> /images/rails.png
[2007-09-04 08:49:59] INFO  going to shutdown ...
[2007-09-04 08:49:59] INFO  WEBrick::HTTPServer#start done.

今日はとりあえずここまで。

Comments are closed.