fc2ブログ

2023.08 «  - - - - - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 » 2023.10
TOP > Rails 2.0・その20(テーブル定義で belongs_to の関係を表すのも簡単に)

 ← Rails 2.0・その21(SOAPの組み込みを外に出した) | TOP | Rails 2.0・その19(migrationカラム指定が簡単に)

Rails 2.0・その20(テーブル定義で belongs_to の関係を表すのも簡単に) 

2008年03月18日 ()
テーブルに依存関係がある場合、属しているテーブルの単数形_id というカラム名で、依存していますよー、ということを表せました。

2.0では、この「属しているテーブルの単数形_id」の代わりに、references や belongs_to を使って属していますということを表すことができます。

Rails 2.0

create_table :trains do |t|
  t.references :railway
  t.string :name, :null => false
  t.text :description
  t.float :max_speed, :null => false
  t.timestamps
end



上記の t.references を、t.belongs_to と書いても同じです。

もしくは、t.references :railway を、 t.integer :railway_id と書いてもやっぱり同じです。

Rails 1.2

create_table :trains do |t|
  t.column, "railway_id", :integer
  t.column, "name", :string, :null => false
  t.column, "description", :text
  t.column, "max_speed", :float, :null => false

  t.column, “created_at”, :datetime

  t.column, “updated_at”, :datetime
end



まあ、小さな違いですが、こういうこともできるということで。


【広告】

[2008.03.18(Tue) 21:41] [2.0]DBTrackback(0) | Comments(0)
↑TOPへ

 ← Rails 2.0・その21(SOAPの組み込みを外に出した) | TOP | Rails 2.0・その19(migrationカラム指定が簡単に)

COMMENT

COMMENT POST















管理者にだけ表示

 ← Rails 2.0・その21(SOAPの組み込みを外に出した) | TOP | Rails 2.0・その19(migrationカラム指定が簡単に)