DataMapper - why "has" and "belongs"?

I'm just getting started with DataMapper, and I'm trying to figure out why you need to specify has and belongs_to .

For example, see an example on the DataMapper website. Isn't that redundant? If Post has n , doesnโ€™t it automatically comment on the belongs_to message? Why should I indicate this?

 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 
+4
source share
3 answers

You specify both sides of the relationship only when you want to use the methods generated by the additional specification. This is completely optional: if you don't need to get to Post from Comment (e.g. @comment.post ), you don't need to specify belongs_to in Comment .

One of the advantages is that your instances are a bit clean, because in Comment additional methods are not generated automatically. On the other hand, if you need them, these additional methods will not bother you.

See also association documentation in ActiveRecord .

+6
source

This gives you ways to easily access a relational object. For example, @post.comments @comment.post . I understand what you mean by using has_many, you could specify the belongs_to property. Although, given the developerโ€™s overhead, adding a role_to property is probably better than adding extra system overhead to dynamically add methods to the correct class.

Another thing will be to use the has_many relation through another has_many relation. This will lead to an odd relationship relationship and is likely to cause problems with SQL.

For instance:

 class User < ActiveRecord::Base has_many :roles, :through => :roles_users has_many :roles_users end 

RolesUser is a connection table that belongs to the _ class for users and role models. Assuming this is the case in this case, add the user's affiliation to the lookup class. And it does not make sense, it also does not work due to the lack of a database column. Of course, this could be adjusted when there is a pass-through option, but again, this will greatly increase the complexity of the code when it is not needed. As Daan said in his answer, you do not need both to work, this is not necessary.

0
source

I would like to add to these good answers that if you required dm-constraints (either directly or via data_mapper ) and use auto_migrate! , then belongs_to will automatically add foreign key constraints at the database level, while has will not.

eg:.

 require 'data_mapper' class Post include DataMapper::Resource property :id, Serial end class Comment include DataMapper::Resource property :id, Serial belongs_to :post end 

Produces this (via the MySQL adapter):

  ~ (0.140343) CREATE TABLE comments (id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, post_id INT(10) UNSIGNED NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci ~ (0.234774) CREATE INDEX index_comments_post ON comments (post_id) ~ (0.433988) ALTER TABLE comments ADD CONSTRAINT comments_post_fk FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE NO ACTION ON UPDATE NO ACTION 

If you use has n, :comments belongs_to :post has n, :comments inside Post , but do not include belongs_to :post inside Comment , the post_id column in the comments table will still be created and indexed, but no foreign key constraint will be added.

0
source

All Articles