我剛剛開始使用DataMapper,並試圖找出爲什麼您需要指定has
和belongs_to
。DataMapper - 爲什麼「有」和「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