A graph with no cycles in it is a tree. So, you need to make sure you're making a tree.
If you're joining "free" objects together, that is, objects that are not already linked to something else, there's no chance of a cycle occurring, clearly. This is also true if you're joining one linked object to a free object.
If you're joining two linked objects to each other, you need to check if they belong to the same tree. If they do belong to the same tree, a cycle will always arise, but if they do not belong to the same tree, a cycle will never arise.
I had this problem when I made my game "penpals". What I did was add a "root object" field, a "parent object" field, and a list of "children" for each object.
When an object is created, its root is itself and its parent is null.
When a free object O1 is linked to another object O2, its root is set to O2's root, O2 becomes O1's parent, and O1 is added to O2's list of children.
When an object which already has a parent is joined to another object which already has a parent, things get trickier. The first object's tree must be "reversed" by adding its old parent to its list of children, then recursively performing this operation on the old parent until you get to a parentless object. The object you want to join is now parentless and can be linked as before.
You can check out my code if you want at
somethingorotherwhatever.com. In fact, this has inspired me to make a more simple example for you. I'll see if I can get that done tonight.