I want user to remove group that user attending groups by removing groupings.
So, I tried write below the code , but when run leave action , it happen error that
Unknown key: group_id.But I don’t know how to deal with it . Please some help.
Thanks in advance.
GroupingsController.rb
def leave
@user = current_user
@group = Group.find(params[:id])
@user.remove(@group)
redirect_to :back , notice: "Destroy!"
end
User.rb
has_many :groups, :through => :groupings,:source => :group
def remove(group)
groupings.find_by_group_id(:group_id => group).destroy
end
# attend method is work correctly.
def attend(group)
groupings.create(:group_id => group)
end
You’ve already specified that you’re using group_id (by saying
find_by_group_id) – so you don’t need to specify it again by passing it as a ‘key’ (eg:group_id =>)So your code should just be