我有一个用户模型,该用户模型可以通过富连接表拥有很多假期。.我在视图上的destroy语句正在从假期表中删除,而不是从user_holidays表中删除。
class HolidaysController < ApplicationController
def destroy
@user = User.find(params[:user_id])
@user_holiday = @user.holidays.find(params[:id])
@user_holiday.destroy
redirect_to @user
end
end
这是查看按钮:
<% @user.holidays.each do |hld| %>
<td><%= hld.name %></td>
<td><%= hld.date %></td>
<td>
<%= button_to('Destroy', user_holiday_path(@user, hld), :method => 'delete', :class => 'btn btn-large btn-primary') %>
楷模:
class User < ActiveRecord::Base
has_many :user_holidays
has_many :holidays, :through => :user_holidays
class UserHoliday < ActiveRecord::Base
attr_accessible :holiday_id, :user_id
belongs_to :user
belongs_to :holiday
class Holiday < ActiveRecord::Base
attr_accessible :name, :date
has_many :user_holidays
has_many :users, :through => :user_holidays
有任何想法吗?谢谢!!!!
最佳答案
您应该使用@user.user_holidays
而不是@user.holidays
@user_holiday = @user.user_holidays.where(holiday_id: params[:id])
关于mysql - 销毁从错误表中删除的操作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10713192/