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.

Reply via email to