我刚刚开始使用 DataMapper,我试图弄清楚为什么你需要指定 ahas
和 a belongs_to
。
例如,查看 DataMapper 网站上的示例。这不是多余的吗?如果发表has n
评论,那么评论不会自动belongs_to
发布吗?为什么我必须指定这个?
class Post
include DataMapper::Resource
property :id, Serial
has n, :comments
end
class Comment
include DataMapper::Resource
property :id, Serial
property :rating, Integer
belongs_to :post # defaults to :required => true
def self.popular
all(:rating.gt => 3)
end
end