2011年9月13日火曜日

rails3 多対多

rails generate scaffold man name:string

rails generate scaffold woman name:string

rails generate migration create_to_man_woman


class Woman < ActiveRecord::Base
  has_and_belongs_to_many :mans
end


class Man < ActiveRecord::Base
  has_and_belongs_to_many :womans
end

class CreateToManWoman < ActiveRecord::Migration
  def self.up
    create_table :men_women, :id => false do |t|
      t.integer :man_id
      t.integer :woman_id
    end
  end

  def self.down
    drop_table :man_woman
  end
end





m = Man.create(:name => 'man1')
w = Woman.create(:name => 'woman1')
m.womans << w


m = Man.create(:name => 'man3')
m.womans << w

0 件のコメント:

コメントを投稿