如果用户是该组的所有者,当我“销毁”组的成员资格时,我需要能够“销毁”组所有权。我有“User”,“Group / Cliq”和“Group / CliqMembership”模型。当用户创建组时,他们是组的“所有者”。然后其他用户可以加入该组。当用户离开组时,该用户和组的成员关联将被销毁。但是,当所有者离开该组时,它只删除“成员资格”而不是“所有权”。我觉得应该有一个简单的解决方案,但我有点卡住了。
为清楚起见:Cliqs = Groups;问题是:如何同时删除所有权关联和成员关联?当“所有者”离开一个团体时,我希望它破坏他们的“团体所有权”和他们的“团体成员资格”。暂时不说:当“所有者”离开时,我如何让拥有的群体“破坏依赖”?
以下是我的模特:
class Cliq < ActiveRecord::Base
belongs_to :owner, class_name: 'User'
has_many :cliq_memberships
has_many :members, through: :cliq_memberships, source: :user
end
class CliqMembership < ActiveRecord::Base
belongs_to :cliq
belongs_to :user
end
class User < ActiveRecord::Base
has_one :owned_cliq, foreign_key: 'owner_id', class_name: 'Cliq', dependent: :destroy
has_many :cliq_memberships
has_many :cliqs, through: :cliq_memberships
.
.
.
end
我的控制员:
class CliqsController < ApplicationController
def show
@cliq = Cliq.find(params[:id])
end
def new
@cliq = Cliq.new(params[:id])
end
def create
@cliq = current_user.build_owned_cliq(cliq_params)
@cliq.members << current_user
if @cliq.save
redirect_to current_user
else
redirect_to new_cliq_path
end
end
def destroy
@cliq = current_user.owned_cliq.find(params[:id])
flash[:alert] = "Are you sure you want to delete your Cliq? Your Cliq and all of its associations will be permanently deleted."
@cliq.destroy
if @cliq.destroy
redirect_to current_user
flash[:notice] = "You deleted the Cliq."
else
redirect_to current_user
#set up error handler
flash[:notice] = "Failed to delete Cliq."
end
end
def cliq_params
params.require(:cliq).permit(:name, :cliq_id)
end
class CliqMembershipsController < ApplicationController
def show
end
def create
#Cliq or Cliq_ID?
@cliq = Cliq.find(params[:cliq])
@cliq_membership = current_user.cliq_memberships.build(cliq: @cliq)
@cliq.members << current_user
if @cliq_membership.save
flash[:notice] = "Joined #{@cliq.name}"
else
#Set up multiple error message handler for rejections/already a member
flash[:notice] = "Not able to join Cliq."
end
redirect_to cliq_url
end
def destroy
@cliq_membership = current_user.cliq_memberships.find(params[:id])
@cliq_membership.destroy
if @cliq_membership.destroy
flash[:notice] = "You left the Cliq."
redirect_to user_path(current_user)
else
end
end
end
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@uploads = Upload.all
@cliq_memberships = CliqMembership.all
@cliqs = Cliq.all
end
end
答案 0 :(得分:0)
在您的CliqMemberships控制器的销毁行动中,在销毁CliqMembership之前,您可以检查当前用户对该组的所有权,就像您正在检查他/她的会员资格一样。我假设你想要一个实现,如果所有者离开该组,你希望该组及其所有成员资格也被自动销毁。 (如果我错了,请纠正我。)在这种情况下,您可以将dependent: :destroy
添加到has_many :cliq_memberships
如果current_user恰好是所有者,你可以完全破坏cliq,这反过来也会破坏它的cliq_membership。
你可以在CliqMemershipsController的破坏行动中这样做。
def destroy
@cliq_membership = current_user.cliq_memberships.find(params[:id])
@cliq = @cliq_membership.cliq
if @cliq.owner == current_user
@cliq.destroy
flash[:notice] = "The Cliq is destroyed"
redirect_to user_path(current_user)
else
# destroy only the membership
flash[:notice] = "You left the Cliq."
redirect_to user_path(current_user)
end
end