rails generate scaffold group name:string
rails generate scaffold menber name:string
rails generate migration group_and_menber
class GroupAndMenber < ActiveRecord::Migration
def self.up
create_table :group_and_menbers do |t|
t.integer :menber_id
t.integer :group_id
end
end
def self.down
drop_table :group_and_menbers
end
end
class GroupAndMenber < ActiveRecord::Base
belongs_to :menber
belongs_to :group
end
class Group < ActiveRecord::Base
has_many :group_and_menbers
has_many :menbers, :through => :group_and_menbers
end
class Menber < ActiveRecord::Base
has_many :group_and_menbers
has_many :groups, :through => :group_and_menbers
end
menber = Menber.new(:name => 'menber1')
menber.save
group = Group.new(:name => 'group1')
group.save
menber.groups << group
で多対多が成立します
rails generate scaffold menber name:string
rails generate migration group_and_menber
class GroupAndMenber < ActiveRecord::Migration
def self.up
create_table :group_and_menbers do |t|
t.integer :menber_id
t.integer :group_id
end
end
def self.down
drop_table :group_and_menbers
end
end
class GroupAndMenber < ActiveRecord::Base
belongs_to :menber
belongs_to :group
end
class Group < ActiveRecord::Base
has_many :group_and_menbers
has_many :menbers, :through => :group_and_menbers
end
class Menber < ActiveRecord::Base
has_many :group_and_menbers
has_many :groups, :through => :group_and_menbers
end
menber = Menber.new(:name => 'menber1')
menber.save
group = Group.new(:name => 'group1')
group.save
menber.groups << group
で多対多が成立します
0 件のコメント:
コメントを投稿