VM上のPostgreSQLにホストから接続
環境
- Ubuntu
- 10.04 LTS
- PostgreSQL
- 8.4
- クライアント
- Windows 7 pgAdminⅢ 1.12.3
設定手順
/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
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 c Loading development environment (Rails 3.0.1) irb(main):001:0> "hoge_foo".camelize => "HogeFoo" irb(main):002:0> "HogeFoo".underscore => "hoge_foo"
tableize
ってのもある。
PoderosaにHomeとEndキーの設定
手順
[ツール] → [オプション] → [操作] → [追加キー定義]
Home=0x1B[1~, End=0x1B[4~
でOK。
アプリ共通の定数
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
- &はmerge()のエイリアス
できること
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)"