Я реализовал отношения пользователь - членство - организация, используя: я использую rolify для реализации ролей пользователя в организациях.
class User < ApplicationRecord
rolify
has_many :memberships, :dependent => :destroy
has_many :organizations, through: :memberships
end
class Membership < ApplicationRecord
belongs_to :user
belongs_to :organization
end
class Organization < ApplicationRecord
resourcify
has_many :memberships, :dependent => :destroy
has_many :users, through: :memberships
end
Затем, всякий раз, когда я вызываю def unjoin в OrganizationControllers:
def join
@organization = Organization.find(params[:id])
if @organization.users.exists?(current_user.id)
flash[:notice] = 'Already a Member'
redirect_to @organization
else
@organization.users << current_user
current_user.add_role :member, @organization
if current_user == @organization.users.last
flash[:notice] = 'Successfully Joined In'
redirect_to @organization
else
flash[:notice] = 'Unable to Join'
redirect_to @organization
end
end
end
def unjoin
@organization = Organization.find(params[:id])
if @organization.users.exists?(current_user.id)
current_user.remove_role :member, @organization #Error
@organization.users.delete(current_user)
if @organization.users.exists?(current_user.id)
flash[:notice] = 'Unable to unjoin'
redirect_to @organization
else
flash[:notice] = 'Successfully Unjoined'
redirect_to @organization
end
else
flash[:notice] = 'Not Yet A Member'
redirect_to @organization
end
end
Я получаю NoMethodError в OrganizationsController # unjoin неопределенный метод `users 'для # , в строке: current_user.remove_role: member, @ organization