我有这些关系的应用程序:
现在,这是模特:
project.rb
class Project
include Mongoid::Document
field :title, type: String
field :description, type: String
field :client, type: String
field :deadline, type: String
belongs_to :owner, :class_name => 'User', inverse_of: :projects
has_many :members, :class_name => 'User', inverse_of: :projects
end
user.rb
class User
include Mongoid::Document
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
## Database authenticatable
field :email, type: String, default: ""
field :encrypted_password, type: String, default: ""
field :username, type: String, default: ""
# Invitations
field :project_invitations, type: Array, default: []
has_many :projects, inverse_of: :user
end
以下是 invites_controller.rb ,用于管理所有邀请系统(接受/拒绝邀请,创建邀请等):
class InvitesController < ApplicationController
def new
@invite = Invite.new(email: params[:search], sender_id: current_user.id)
end
def create
@project = Project.find(params[:project_id])
recipient = User.find_by(email: params[:search])
@invite = Invite.new(email: params[:search], sender: current_user.id, recipient: recipient, project_id: @project, accepted: false)
if @invite.save
flash[:success] = 'the invitation is sent'
recipient.push(project_invitations: @invite._id )
recipient.save
redirect_to project_path(@project)
else
flash[:error] = 'something went wrong'
puts @invite.errors.full_messages
render :back
end
end
def accept
@invite = Invite.find_by(id: params[:project_id])
@user = current_user
@invite.accepted = true
@user.project_invitations.delete(@invite._id)
@invite.save
@user.save
@project.member = @user
end
def decline
@invite = Invite.find_by(id: params[:project_id])
@user = current_user
@project = Project.find(params[:project_id])
@invite.accepted = false
@user.project_invitations.delete(@invite._id)
@invite.save
@user.save
end
end
以下是问题:
has_many :members
,但我不知道如何设置普通用户成为:member .. 修改
你认为创建一个数组字段应该是一个很好的ID我会把所有users_id(谁是成员)?
谢谢!
答案 0 :(得分:1)
会员资格与特定项目有关。您的项目有许多成员,需要以某种方式反映其数据结构。我设想一个包含一堆用户外键和项目外键的成员表。然后,您可以尝试通过userID和projectID查找成员,以确定他们是否在任何时候都是成员?虽然不太确定,但我还没有经验,所以我很想听听其他人说的话。