DataMapper Many-to-Many Delete Constraint - ruby

Let's say we have two models with a many-to-many relation:
class Tag
include DataMapper::Resource
property :id, Serial
has n, :articles, through: Resource
end
class Article
include DataMapper::Resource
property :id, Serial
has n, :tags, through: Resource
end
Now if I create an article with a tag: Tag.create(articles: [ Article.create ])
If I run Tag.first.delete now, it returns false since there is a foreign key constraint due to the many-to-many relationship. If I run Tag.first.delete! it deletes the Tag but not the association record in the article_tags table.
If I use dm-contraints and set everything to :destroy it also destroys the Article which is not what I want.
I can do
tag = Tag.first
tag.articles = []
tag.save
tag.destroy
but this seems seems unclean. Is there a better way?

Since Tag and Article are linked through a many-to-many relationship, you'll need to first destroy any 'ArticleTag' join model that references the object you're trying to delete.
#get the tag to delete
tag = Tag.first
#deletes all rows in the article_tags table that reference
#the tag you want to delete
ArticleTag.all(:tag => tag).destroy
#an alternative to the line above--it does the same thing
tag.article_tags.all.destroy
#another alternative--it won't delete the articles only
#the join model that references the tag
tag.articles.all.destroy
#finally, obliterate the tag
tag.destroy

Related

How to make a Many-To-One relationship in DataMapper

I'm trying to create an association between two models:
class Person
include DataMapper::Resource
property :id, Serial
property :name, String
end
class Country
include DataMapper::Resource
property :id, Serial
property :name, String
end
I just need a simple relationship on Person (a country_id).
My first idea was to put a has 1 property on Person:
class Person
include DataMapper::Resource
property :id, Serial
property :name, String
has 1, :country
end
Instead of a country_id on Person table, Datamapper created a person_id on Country.
To get what I need, I had to make it inverse:
class Country
include DataMapper::Resource
property :id, Serial
property :name, String
has 1, :person
end
This way I got my country_id field on Person table, but it really doesn't make any sense for me.
Am I misunderstanding something or is there another way to make this association?
Whenever Model A "has" Model B (either one or many), you add the foreign key to Model B.
The flipside of this is "belongs to" - that's what you put on the Model with the foreign key.
I guess in DataMapper you don't have to add foreign key columns explicitly, but you could still do it if you want.
# Person
property :country_id, Integer
Since the foreign key in on Person, you'd use "belongs to". It seems like the same thing as "has one", but it's not. You generally only need "has one" in special cases like one-to-one relationships.
# Person
belongs_to :country
# Country
has n, :people
# you could use has 1, :person if for some reason every country
# only had 1 person in it

DataMapper: one-to-many relationship with custom name?

I'm trying to build a small model consisting of two entities. For the purposes of this question, call them A and B. A has a one-to-many relationship to several Bs; this means that each B belongs_to an A.
In this particular case, I'd like to call the relationship from B back to A something other than a. I think I got close with the following:
class A
include DataMapper::Resource
property :id, Serial
has n, :bs
end
class B
include DataMapper::Resource
property :id, Serial
belongs_to :owner, 'A'
end
The important bit here is the belongs_to :owner, 'A' line in B. With this, I can successfully:
Create and save an instance of A
Query that A for its bs and get an empty array back
Create an instance of B, assigning its owner to be the A I made earlier
However, when I go to save that instance of B, I run into trouble – calling save returns false. If I print the B, I see that it has two attributes: one called owner_id and another called a_id.
What else do I need to do with this model to rename the relationship from B back to A? Is such a rename even possible?
Figured it out. The owning entity (A) needs to explicitly specify the child keys that it wants created for the relationship:
class A
include DataMapper::Resource
property :id, Serial
has n, :bs, :child_key => [ 'owner_id' ]
end
class B
include DataMapper::Resource
property :id, Serial
belongs_to :owner, 'A'
end
With this change, I only see one relationship attribute created on B, and I'm able to save instances of B that I create.

DataMapper: one to many relationship with one of several models?

Here's my models:
class Item
include DataMapper::Resource
property :id, Serial
has 1, :firstitem
has 1, :seconditem
end
class FirstItem
include DataMapper::Resource
property :id, Serial
belongs_to :item
end
class SecondItem
include DataMapper::Resource
property :id, Serial
belongs_to :item
end
Now, my question is this - if I want FirstItem and SecondItem to be different models but want them both to potentially be part of Item (but only one of the two, so a record with FirstItem will not also have a SecondItem), I could make a has 1 relationship for both of them, and only one of them gets filled.
So in a relational database, does it make sense to do this? Is there a better, more efficient way of defining this relationship?
What you want is a polymorphic association, which sadly, DataMapper does not support. Try looking into ActiveRecord instead; you can easily use it with Sinatra.

Foreign keys and associations using DataMapper

I have read this page quite thoroughly:
http://datamapper.org/docs/associations
If the answer is on there, it's simply not expressed in a way I can understand.
I'm very confused about using setting up relationship via Datamapper. The datamapper site above is pretty much all I can find on the topic, and as I've said, it hasn't been particularly helpful.
For example, if I want to create something like the following:
Table: users
id (primary key)
name
Table: attributes
id (pk)
title
Table: user_attributes
id (pk)
user_id (fk to users.id)
attribute_id (fk to attributes.id)
value
This seems simple enough, but it has been prohibitively difficult. Everything I try gives me errors like No relationships named user_attributes or user_attribute in UserUserAttribute (DataMapper::UnknownRelationshipError)
Can someone please tell me the class definitions for this simple mapping, and perhaps point me to a better discussion of DataMapper associations? Below is some of what I've tried.
class User
include DataMapper::Resource
property :id, Serial, :key => true
property :name, String
has n, :user_attributes, :through=>:attribute
end
class Attribute
include DataMapper::Resource
property :id, Serial, :key => true
property :name, String
has n, :user_attributes, :through=>:user
end
class UserAttribute
include DataMapper::Resource
belongs_to :user
belongs_to :attribute
end
I think you're seeing things like UserUserAttribute because DataMapper is trying to auto-generate an anonymous join class.
Here's an article that describes how to make named many-to-many relationships in DataMapper
You would probably change the example something like this:
User -> User
Project -> Attribute
Collaboration -> UserAttribute
The issue is that your class is UserAttribute, and you're trying to name the relationships with user_attributes. Get rid of the underscore, and your issue will disappear (or add an underscore to the class name)

DataMapper subclassing & many-to-many self-referential relationships

I'm building a small Ruby application using DataMapper and Sinatra, and I'm trying to define a basic blog model:
The blog has multiple Users
I have a collection of Posts, each of which is posted by a User
Each Post has a set of Comments
Each Comment can have its own set of Comments - this can repeat several levels deep
I'm running into trouble getting the self-referential relation between comments going due to the fact that each Comment belongs_to a Post. My classes right now look like this:
class User
include DataMapper::Resource
property :id, Serial
property :username, String
property :password, String
has n, :post
end
class Post
include DataMapper::Resource
property :id, Serial
property :content, Text
belongs_to :user
has n, :comment
end
class Comment
include DataMapper::Resource
property :id, Serial
property :content, Text
belongs_to :user
belongs_to :post
end
I'm following the guide at Associations and building a new object (CommentConnection) to link two comments together, but my issue is that each subcomment shouldn't belong to a Post as implied by the Comment class.
My first instinct was to extract out a superclass for Comments, so that one subclass could be "top-level" and belong to a post, while the other kind of comment belongs to another comment. Unfortunately, when I do that I run into issues with the comment IDs becoming null.
What's the best way to model this kind of recursive comment relationship in DataMapper?
What you need is a self referential join in Comments, e.g., each Comment can have a parent comment. Try the following:
class Comment
include DataMapper::Resource
property :id, Serial
property :content, Text
has n, :replies, :child_key => [ :original_id ]
belongs_to :original, self, :required => false #Top level comments have none.
belongs_to :user
belongs_to :post
end
This will allow you to have replies to any given comment, although accessing them may get a little nasty (slow) if the volume gets high. If you get this working and want something more sophisticated you could look at nested sets, I believe there is a nested sets plugin for DataMapper but I haven't used.

Resources