DataMapper : one-to-many

require 'rubygems'
require 'dm-core'
require 'dm-migrations'

#'mysql://user:password@hostname/database'
DataMapper.setup :default, "mysql://root:123456@localhost/cc"


class User
  include DataMapper::Resource

  property :id , Serial
  has n, :comments
end

class Comment
  include DataMapper::Resource
   property :id, Serial
   belongs_to :user
end

DataMapper.auto_migrate!
user = User.create
comment1 = Comment.create
comment2 = Comment.create
user.comments << comment1
user.comments << comment2
user.save
puts User.all
puts Comment.all

 
DataMapper : one-to-many
 


DataMapper : one-to-many
 

http://datamapper.org/docs/associations.html

你可能感兴趣的:(datamapper,one-to-mant)