VM上のPostgreSQLにホストから接続

環境

Ubuntu
10.04 LTS
PostgreSQL
8.4
クライアント
Windows 7 pgAdminⅢ 1.12.3

設定手順

/etc/postgresql/8.4/main/postgresql.conf 編集

59行目あたり

listen_addresses = '*'

コメントアウトを解除して、* に変更。

/etc/postgresql/8.4/main/pg_hba.conf 編集

86行目あたり

host    all         all         192.168.198.0/24      trust

を追加。
※Windows側のコマンドプロンプトでipconfig /all の結果から、

イーサネット アダプター VMware Network Adapter VMnet8:
〜省略〜
IPv4 アドレス . . . . . . . . . . : 192.168.198.1(優先)

の結果から。

PostgreSQL再起動
$ sudo /etc/init.d/postgresql-8.4 restart

参考URL

Rails3でページング

その名は kaminari。あとで。

Gemfileに
gem 'kaminari'
して
$ bundle

追記 2011/08/06

使ってみた。

# Japanese translations for Ruby on Rails
# by Akira Matsuda (ronnie@dio.jp)
# AR error messages are basically taken from Ruby-GetText-Package. Thanks to Masao Mutoh.
# contributors:
#  - Tsutomu Kuroda (t-kuroda@oiax.jp)

ja:
〜〜〜省略〜〜〜
  views:
    pagination:
      previous: "<< 前ページ"
      next: "次ページ >>"
      truncate: "..."
      first: "最初のページ"
      last: "最後のページ"

通貨のフォーマット

環境

Rails
3.0系

やり方

設定(config/locales/ja.yml)
    currency:
      format:
        format: "%n%u"
        unit: "円"
        separator: "."
        delimiter: ","
        precision: 0
        significant: false
        strip_insignificant_zeros: false

precision を 3 から 0 に変更します。

表示(erb)
<%= number_to_currency(100000) %>

で「100,000円」と表示されます。

アプリ共通の定数

config/initializers/constants.rb
を作成。

constants.rb
# -*- coding: utf-8 -*-
# 共通で利用する定数を定義
module Constants
  HOGE = 1
  FOO  = 2
end
Constants::HOGE

で参照できます。

named_scopeについて

環境

Rails
3.0.8

確認用モデル生成

author
$ rails g model author name:string 
book
$ rails g model book author:references title:string price:integer

author:referencesで生成するとBookモデルはbelongs_to :author ができます。

class Book < ActiveRecord::Base
  belongs_to :author
end

モデルのnamed_scope

author
class Author < ActiveRecord::Base
  scope :name_is, lambda {|n| where :name => n}
end
book
class Book < ActiveRecord::Base
  belongs_to :author
  scope :cheaper_than,
    lambda {|p| where 'price <= ?', p}
  scope :written_by,
    lambda {|name| joins(:author) & Author.name_is(name)}
end

できること

Book.cheaper_than(1000).written_by('david').to_sql
 => "SELECT \"books\".* FROM \"books\" INNER JOIN \"authors\" ON \"authors\".\"id\" = \"books\".\"author_id\" WHERE \"authors\".\"name\" = 'david' AND (price <= 1000)"