WebOS Goodies へようこそ! WebOS はインターネットの未来形。あらゆる Web サイトが繋がり、共有し、協力して創り上げる、ひとつの巨大な情報システムです。そこでは、あらゆる情報がネットワーク上に蓄積され、我々はいつでも、どこからでも、多彩なデバイスを使ってそれらにアクセスできます。 WebOS Goodies は、さまざまな情報提供やツール開発を通して、そんな世界の実現に少しでも貢献するべく活動していきます。
これまで、modelと一緒に自動生成されるもの以外の、カラムを追加する類いのマイグレーションファイルは手書きしていたんだけれど、どうやらrails2.0以降はこれも自動化できるみたい。というわけでやり方をメモ。 追加するとき 例 こんな風に書くと script/generate migration AddEmailAndDeletedToUsers email:string deleted:boolean こんなファイルが出来る。 class AddEmailAndDeletedToUsers < ActiveRecord::Migration def self.up add_column :users, :email, :string add_column :users, :deleted, :boolean end def self.down remove_column :users,
既存のテーブルに列を追加したい場合、ジェネレーターで migration add_column とかやるのだけど、Rails 2.0 では少し便利になって、以下のように書けるらしい。 >script/generate migration AddPriceToProduct price:decimalAddカラム名Toモデル名、の後にカラムのタイプ指定というフォーマット。これを実行すると db/migrate/20080427070207_add_price_to_product.rb のような名前で以下のマイグレーションファイルが自動生成される。 class AddPriceToProduct < ActiveRecord::Migration def self.up add_column :products, :price, :decimal end def self.down remo
scaffoldはモデル同士の関連は無視して*1、最も基本的なwebアプリケーションとしての骨格を生成してくれる。自分ではそれをお手本に、少しずつ拡張していくことが多い。しかし、モデル同士が無関係という状況はほとんどなく、ほとんど例外なくモデルにhas_manyやhas_one、belongs_toと書いて、毎回同じような修正をして行くことが多い。(そして、ちょっとした間違いでエラーをもらうことが多い。)そんな時、もしscaffoldがやってくれたら、どんなコードを生成してくれるのだろう?Rails2.1でサンプルプロジェクト「Q&Aサイト」を作りながら、試行錯誤してみた。 いつもの手順 $ rails QandA $ script/generate scaffold question name:string body:text $ script/generate scaffold ans
migration ファイルが db/migrate ディレクトリの下に作成されたり、自分で作成したりします。 そのファイル中でのカラムの書き方がとても楽になりました。 例えば、books テーブルのカラムが title, first_name(20文字まで), last_name(20文字まで), price, created_at, updated_at だったとしたら、↓のように書くことができます。 [Rails2.0] create_table :books do |t| t.string :title t.string :first_name, :last_name, :limit => 20 t.integer :price t.timestamps end timestamps っていう一行で、created_at カラムと update_at カラムの2つのカラムが作成され
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く