Ruby on rails Rails-销毁用户组中的成员资格/所有权
如果用户是组的所有者,则在“销毁”组的成员身份时,我需要能够“销毁”组所有权。我有“用户”、“组/Cliq”和“组/CliqMembership”模型。当用户创建组时,他们是组的“所有者”。然后,其他用户可以加入该组。当用户离开组时,该用户和组的成员关联将被销毁。但是,当所有者离开组时,它只删除“成员资格”,而不删除“所有权”。我觉得应该有一个简单的解决办法,但我有点卡住了 为清楚起见:Cliqs=组;问题是:如何同时删除所有权协会和会员协会?当一个“所有者”离开一个组时,我希望它摧毁他们的“组所有权”和“组成员资格”。旁白:当“所有者”离开时,我将如何使所拥有的组“摧毁依赖性” 以下是我的模型:Ruby on rails Rails-销毁用户组中的成员资格/所有权,ruby-on-rails,ruby-on-rails-4,activerecord,associations,Ruby On Rails,Ruby On Rails 4,Activerecord,Associations,如果用户是组的所有者,则在“销毁”组的成员身份时,我需要能够“销毁”组所有权。我有“用户”、“组/Cliq”和“组/CliqMembership”模型。当用户创建组时,他们是组的“所有者”。然后,其他用户可以加入该组。当用户离开组时,该用户和组的成员关联将被销毁。但是,当所有者离开组时,它只删除“成员资格”,而不删除“所有权”。我觉得应该有一个简单的解决办法,但我有点卡住了 为清楚起见:Cliqs=组;问题是:如何同时删除所有权协会和会员协会?当一个“所有者”离开一个组时,我希望它摧毁他们的“组
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
类Cliqclass 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
class CliqsController @cliq.members在您的CliqMemberships控制器的销毁操作中,在销毁CliqMembership之前,您可以检查当前用户对组的所有权,就像检查他/她的成员资格一样。我假设您想要一个实现,其中如果所有者离开组,您也希望组及其所有成员身份自动被销毁。(如果我错了,请纠正我。)在这种情况下,您可以将dependent::destroy
添加到has\u many:cliq\u成员身份
如果当前用户恰好是所有者,您可以完全销毁cliq,这反过来也会破坏其cliq\U成员身份
您可以在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
我知道我错过了最后一块。这很有魅力!谢谢!