Question

I have a problem with simple delete action.

I have a view


%p
  Name:
  = @element.name
%p
  Description:
  = @element.description
%p
  = link_to 'edit', edit_element_path(@element)
  |
  = link_to 'delete', @element, :confirm => 'Are you shure?', :method => :delete

When I try to click on that delete in browser and clik OK it says that it can not delete from unexisting table elements_muscle_groups, why does he tries to delete from that unexisting table anyway?

By the way I am using Rails 4

You can see the controller code on screenshot.

Sorry for eng and ty for help.

enter image description here

EDIT: Hi I have one more thing to add,

Element has_and_belongs_to_many :muscle_groups

MuscleGroup has_and_belongs_to_many :elements

so maybe it should delete from elements_muscle_groups, but why does that table does not exist then?

Was it helpful?

Solution

from rails api:

http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_and_belongs_to_many

has_and_belnogs_to_many: Specifies a many-to-many relationship with another class. This associates two classes via an intermediate join table. Unless the join table is explicitly specified as an option, it is guessed using the lexical order of the class names. So a join between Developer and Project will give the default join table name of “developers_projects” because “D” outranks “P”.

so you need to make sure you have a table called

elements_muscle_groups with element_id: integer and muscle_group_id:integer

OTHER TIPS

I didn't use Rails 4 much, But I think you are trying to achieve it wrongly: your destroy methods should look like this :-

def destroy 
 @element = Element.find(params[:id])
 if @element.destroy
   flash[:message] = "Record destroy successfully."
   redirect_to elements_path
 else
   flash[:error] = "Record destroy successfully."
   render element_path(@element)
 end
end

You could change the method accordingly. Hope it will help.

Migration file will be look like this,

class CreateElementsMuscleGroups < ActiveRecord::Migration
  def change
    create_table :elements_muscle_groups do |t|
      t.integer  :element_id
      t.integer  :muscle_group_id
      t.timestamps
    end
  end
end

Run this migration.. That will solve the issue..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top