I had the same problem, and the docu is scarce on that.
If you have an m:n relationship through Resource:
some_post.labels.intermediaries.delete_if{|i| i.label ==
some_label}
[and maybe some sort of save?]
But I think, that wouldn't be a resourceful solution, because all
labels will be loaded.
However, the trick with :through => Resource is the 'intermediaries'
method, referencing the jointable, even if you don't have a ruby class
for it.
Maybe something in the lines of:
some_post.labels.intermediaries.get(some_post.id,
some_label.id).destroy!
the problem I see with that is, that you have to figure out if (in the
get method call) post-ids come first, or label-ids.
I needed to destroy all links to the associated labels for the given
post, and I did
some_post.labels.intermediaries.destroy!
All of this solutions just destroy the joint, never the actual
associated data.
I am really just a noob with datamapper myself, so no guaranties, then
any of this stuff works for your case, sry :)
~jarod
On Mar 13, 6:15 am, bl <[email protected]> wrote:
> I figured it out... sorta:
>
> When I'm looping through the labels for a post, I can correctly do
> some_post.labels.each..., but I CAN'T destroy an element from this
> collection without destroying the real resource (the underlying
> label). So what I had to do was create a Tagging join table and do
> some_post.taggings(:label_id => an_id).destroy, BUT you can't
> reference label_id on the model without explicitly setting it up as an
> Integer property (even though it's there implicitly).
>
> This all seems counter-intuitive to me so I'm probably doing something
> very n00bish and wrong, but it works.
>
> On Mar 12, 11:08 pm, bl <[email protected]> wrote:
>
>
>
> > OK some more info...
>
> > I have an m:n relationship between Posts and Labels using a Tagging
> > table. If I try to do:
>
> > post.labels(:name => "a name").destroy
>
> > It removes both the tagging AND the actual label from the database, so
> > now no posts have that label (because the label doesn't exist
> > anymore). This doesn't seem like the right behavior.
>
> > On Mar 12, 10:17 pm, bl <[email protected]> wrote:
>
> > > Thanks for all the help. I have a :through => Resource association so
> > > I'll try to grab the right label object and then set the post_id to
> > > nil as suggested. If that doesn't work, I'll create an explicit
> > > associations table so I can get to it.
>
> > > On Mar 12, 7:50 pm, Martin Gamsjaeger <[email protected]> wrote:
>
> > > > If you have a m:n association
>
> > > > Post.has n, :post_labels
> > > > Post.has n, :labels, :through => :post_labels
>
> > > > # safe - destroy intermediate resource after loading it (with
> > > > validation and callbacks)
> > > > post.post_labels.get(1).destroy
> > > > # unsafe - destroy intermediate resource directly without loading it
> > > > and invoking callbacks
> > > > post.post_labels.get(1).destroy!
>
> > > > If you have a 1:n association
>
> > > > Post.has(n, :labels)
>
> > > > # safe - destroy target resource after loading it (with validation and
> > > > callbacks)
> > > > post.labels.get(1).destroy
> > > > # unsafe - destroy target resource directly without loading it and
> > > > invoking callbacks
> > > > post.post_labels.get(1).destroy!
>
> > > > The key here is that the #destroy and #destroy! methods are available
> > > > both on Resource and on Collection. If you want to destroy a single
> > > > resource, you just call #destroy on it, if you need to destroy a
> > > > single resource inside a collection you do
> > > > post.labels.get(1).destroy, again calling #destroy on a Resource that
> > > > you first need to get to. If you want to destroy a complete
> > > > collection, do post.labels.destroy. This will destroy all labels
> > > > associated with post. If you only want to destroy some elements inside
> > > > a collection, do post.labels(:color => 'blue').destroy
>
> > > > All you need to do is make sure you either call #destroy or #destroy!
> > > > on a Resource, or on a Collection. A "belongs_to" relationship gives
> > > > you "almost" a Resource (it's a proxy internally), a "has"
> > > > relationship gives you a Collection, as will any call to #all, with or
> > > > without conditions.
>
> > > > HTH
> > > > cheers
> > > > snusnu
>
> > > > On Sat, Mar 13, 2010 at 01:54, Tony Mann <[email protected]> wrote:
> > > > > Or use the model LabelsPosts (or some name similar to this) and
> > > > > delete the
> > > > > join record.
> > > > > ..tony..
>
> > > > > On Fri, Mar 12, 2010 at 2:51 PM, arbales <[email protected]> wrote:
>
> > > > >> Are the posts and labels associated through a join table?
>
> > > > >> class Post
> > > > >> ...
> > > > >> has n, :labelings
> > > > >> has n, :labels, :through => :labelings
> > > > >> end
>
> > > > >> label_to_remove = Label.get(id)
> > > > >> some_post.labelings.first(:label => label_to_remove).destroy
>
> > > > >> On Mar 11, 12:18 pm, bl <[email protected]> wrote:
> > > > >> > I can't find this anywhere.
>
> > > > >> > Let's say you're making the classic "blog" style app and you have
> > > > >> > Posts with labels. Well I know you can do:
>
> > > > >> > some_post.labels << some_label
> > > > >> > some_post.save
>
> > > > >> > But it seems like you can't do:
>
> > > > >> > some_label = Label.get(id)
> > > > >> > some_post.labels.remove(some_label)
> > > > >> > some_post.save
>
> > > > >> > remove() throws a type error, delete_if doesn't enumerate, etc.
>
> > > > >> > I must be missing something here. This would seem to be an
> > > > >> > essential
> > > > >> > feature of associations but there's no documentation for it and web
> > > > >> > searches turn up almost nothing.
>
> > > > >> --
> > > > >> 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.
>
> > > > > --
> > > > > 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.
--
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.