On 10 Sep 2009, at 16:05, Jos Elkink wrote: > I have a class Item, which can be part of class Character, but might > not be. > > I.e. in character.rb > has n, :items > > and in item.rb > belongs_to :character > > Now I can simply add an item by doing > c.items << item > > but I don't seem to be able to disassociate an item from a > character, i.e. > c.items.delete(item) > > Which I thought should work. I don't get an error message, but the > item record in the database still refers to the character, and > c.items.all still includes the item. How should I remove the > association?
Hi Jos In this situation, the association is a property of the Item, not a separate fact in itself. The only way to have a free-floating Item is to set its character_id to nil (NULL in the database). I can't actually say whether #delete should or should not work in this situation. Because I work to avoid NULLs, in this situation I define a table in the middle, eg item_ownerships(owner_character_id, item_id). This allows you to have Characters and Items independently, and have a separate store of facts about who owns what item. However, if only one Character may have an item, you have to make the item_id unique, and enforce that in your domain model. The advantage is you can more easily build other relationships between the two sets of objects, eg item_manufacturings(creating_character_id, item_id), to describe which character (if any) made an item. The principle here is that lots of small tables in the database allow you to be more flexible and explicit about what facts you're asserting, the downside is you have to do more work in the domain model code to handle it. Let me know if I didn't put the ideas in this or my previous email across well, I'm not sure I explained them as simply as they could be. Ashley -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashleymoran http://aviewfromafar.net/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "DataMapper" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/datamapper?hl=en -~----------~----~----~----~------~----~------~--~---
