You (effectively) delete the intermediate object. You could try one of two 
things:

@person.pictures -= [@picture]

If all the items are persisted, then that should delete the join object 
immediately. (Just tested, it did work here.)

@person.pictures = @person.pictures.to_a.reject{ |p| p == @picture }

(Long-hand way to do the same thing)

Now if you have a UI around this, what you would do is build an array of 
checkboxes with the picture IDs in them, and then use the helper method 
@person.picture_ids=(array of ids) that the association built for you. You 
don't need to do any of the above long-hand.

So in your controller, you would add picture_ids: [] to the end of your list of 
allowed attributes in the strong parameters. Next, you would create a checkbox 
for each attached image in your form:

<%- @person.pictures.each do |picture| %>
<%= check_box_tag 'person[picture_ids][]', picture.id, true %>
<%= image_tag picture.file_url %> (just guessing how your internals look, do 
something here to show a thumbnail)
<%- end %>

And that should do the whole thing for you.

Walter

> On Feb 5, 2018, at 1:18 AM, fugee ohu <fugee...@gmail.com> wrote:
> 
> So if @person.pictures << @picture adds a picture to a person, how do you 
> remove a picture from a person?
> 
> 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/B5459EBB-B5D7-4CCD-B612-F83453637C56%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to