update_attributes doesn't remove elements (or what's a proper way to represent this relationship?)

92 views
Skip to first unread message

dblock

unread,
Apr 22, 2011, 12:00:15 PM4/22/11
to Mongoid
I have a many-to-many relationship between Exhibitor and Artwork. An
exhibitor can be showing an artwork at an exhibition and artworks
travel with many exhibitors. I tried many relationships and the
closest I got things to work is with a
references_and_referenced_in_many :artworks inside Exhibitor:

class Exhibitor
...
references_and_referenced_in_many :artworks
end

(no inverse relationship defined, although that makes no difference)

I am saving via a controller that's called by javascript. With this, I
can do:

1) create(hash) where hash contains artworks = [ array of ids ] ->
success
2) update_attributes(hash) where hash contains artworks = [ array of
ids, with several new ids ] -> success
3) update_attributes(hash) where hash contains artworks = [ array of
ids, with removed ids ] -> fail

By fail I mean nothing is removed. Is this a bug? Am I doing something
wrong - is there a better/proper way to represent this relationship? I
am on rc8.

Steve V

unread,
Apr 22, 2011, 3:07:18 PM4/22/11
to Mongoid
I think this is a bug. At least in terms of checking in the rails
console. The update is actually happening DB side, but the calling
object does not have its references updated. Once I reload the object
from the DB, then the updates are reflected. This is definitely not in
keeping with expected behavior. I tested with 2.0.1.

Steve V

unread,
Apr 22, 2011, 4:06:18 PM4/22/11
to Mongoid
Hopefully one of the core committers can comment on this. After going
through the code with rdb, I think the proper place to make the change
for this would be in Dirty#modify. As it is now, it just sets the
relationship attribute(in this case :artwork_ids), and calls it a day.
It seems like it should instead pull the field information, and check
if it's dealing with a relation. If that's the case it should grab
model instances for given ids, and set them for the
relation(artworks).

Perhaps this behaviour is already encapsulated elsewhere, and could be
added with a simple call. I also don't know if there are any gotchas
to this offhand. Again, maybe this is something that David, or Kieran
can provide more insight on.

Kieran P

unread,
Apr 22, 2011, 10:49:52 PM4/22/11
to mon...@googlegroups.com

Steve V

unread,
Apr 22, 2011, 11:36:41 PM4/22/11
to Mongoid
It does seem related to 605, though I think in the scenario with a
relation using *_ids, it's a case of mongoid not even attempting to
load the relation models. The data does save correctly to the DB, so I
don't think 814 applies. I could of course be completely wrong,
because going through the saving code is quite a complicated process.

dblock

unread,
Apr 25, 2011, 10:53:01 AM4/25/11
to Mongoid
Reply all
Reply to author
Forward
0 new messages