I have a note model with the following association
note.rb
has_many :note_categories, :dependent => :destroy
has_many :categories, :through => :note_categories
The NoteCategory model was created to work as a table of links between notes and categories. Originally it was only a model / table, but I created a controller to do some custom things when someone removes a category from a note.
note_categories_controller.rb
def destroy
p "in notes_categories_controller destroy"
note_category_to_delete = NoteCategory.find(params[:id])
note_category_to_delete.destroy
respond_to do |format|
format.html { redirect_to(notes_url }
format.xml { head :ok }
end
end
This works great because I can use this link to create a button that will remove the category from the note:
<%= button_to 'Remove', note_category, :confirm => 'Are you sure?', :controller => :note_categories, :method => :delete %>
and it works great.
, , note_category, , , destroy . , , . :
Note Load (0.7ms) SELECT * FROM "notes" WHERE ("notes"."id" = 245)
NoteCategory Load (0.5ms) SELECT * FROM "note_categories" WHERE ("note_categories".note_id = 245)
NoteCategory Destroy (0.3ms) DELETE FROM "note_categories" WHERE "id" = 146
Note Destroy (0.2ms) DELETE FROM "notes" WHERE "id" = 245
, : depend = > : destroy, destroy NoteCategories . ?