使用rails 5上的资源构建许多版本的用户

时间:2017-11-06 14:56:42

标签: ruby-on-rails ruby ruby-on-rails-5

我在创建通知时遇到问题。通知宝石只有在评论创建和更新时创建通知才能正常工作。帖子创建和更新怎么样?这就是问题所在。我有一个模型mycase,有评论和任务。 通知在评论和任务上运行良好,但在创建和更新mycase时却没有。我必须多次构建用户的diff版本,但它无法实现。我不知道如何在轨道上完成这项工作。我有来自同一模型和类User的admin,adminassistance,attorny_user和client。 我正在使用activity_notification gem。 这是我的user.rb

class User < ApplicationRecord
belongs_to :lawfirm
has_many :mycases
has_many :tasks
has_many :mycasecomments
has_many :posts
has_many :comments
acts_as_target
rolify

devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
end

这是我的mycase.rb

class Mycase < ApplicationRecord
enum status: [:open, :under_review, :pending, :completed, :closed]
has_many :mycasecomments, dependent: :destroy
has_many :tasks, dependent: :destroy
belongs_to :lawfirm
belongs_to :attorney_user, class_name: 'User', foreign_key:     :attorney_user_id, optional: true
belongs_to :client, class_name: 'User',foreign_key: :client_id, optional: true    


belongs_to :admin,  class_name: 'User',foreign_key: :admin_id,optional: true, optional: true
belongs_to :adminassistance, class_name: 'User',foreign_key: :adminassistance_id, optional: true
attr_readonly :caseNo


has_many :mycasecommented_users, through: :mycasecomments, source: :user
has_many :tasked_users, through: :tasks, source: :user

acts_as_notifiable :users,
#Notification targets as :targets
# Set to notify to author and users commented to the article, except    comment owner self
targets: ->(mycase, key) {
  ([mycase.attorney_user] + [mycase.client]+[mycase.admin] +[mycase.adminassistance]).uniq
},
# Path to move when the notification is opened by the target user
# This is an optional configuration since activity_notification uses polymorphic_path as default
#tracked: true,
notifiable_path: :mycase_notifiable_path

def mycase_notifiable_path
  mycase_path(self.id)
end


def start_time
    self.arrival_date ##Where 'start' is a attribute of type 'Date' accessible through MyModel's relationship
end

def end_time
    self.due_date ##Where 'start' is a attribute of type 'Date' accessible through MyModel's relationship
end

end

这是mycases_controllers

class MycasesController < ApplicationController
load_and_authorize_resource :mycase

skip_before_action :authenticate_user!, :only => [:index, :show]
before_action :set_mycase, only: [:casetask, :meet, :show, :edit, :update, :destroy]


.......  

def create
  @users = User.all
    @users.each do |user| 
    if  user.has_role? :adminassistance, current_user.lawfirm  
        @adminassistance_id = user.id
        @adminassistance = user
        #@adminassistance = user
        #user.mycases.build(mycase_params)
    elsif user.has_role? :lawfirmadmin, current_user.lawfirm   
       @admin_id = user.id
       @admin = user
       #@admin = user
      #user.mycases.build(mycase_params)
    end
  end
    #@mycase.lawfirm_id = current_user.lawfirm_id
     @attorney_user = User.find_by(params[@mycase.attorney_user_id])

    @client = User.find_by(params[@mycase.client_id])

    @mycase.admin_id = @admin_id
    @mycase.adminassistance_id  = @adminassistance_id

    @mycase.status= 0
    @mycase = @admin.mycases.build(mycase_params)
    @mycase = @adminassistance.mycases.build(mycase_params)
    @mycase = @client.mycases.build(mycase_params)

    @mycase= @attorney_user.mycases.build(mycase_params)
    @lawfirm = current_user.lawfirm
    @mycase.lawfirm_id=  @lawfirm.id

    @mycase= @lawfirm.mycases.create(mycase_params)
    @mycase.notify :users, key: "Legal Case Created, you are associated"



     respond_to do |format|
        if @mycase.save
        format.html { redirect_to @mycase, notice: 'Legal Case was successfully created.' }
        format.json { render :show, status: :ok, location: @mycase }
        else

        format.html { render :new }
        format.json { render json: @mycase.errors, status:            :unprocessable_entity }

      end
      end

      end






    def mycase_params

      params.require(:mycase).permit(:caseNo, :client_name, :subject,      :area_of_practice, :opponents, :assigned_attorney, :arrival_date, :due_date, :status, :summary_of_brief, :add_judge, :add_court, :client_id, :lawfirm_id, :attorney_user_id, :admin_id, :adminassistance_id)
   end
   end

Iam具有未知属性&#39; user_id&#39;对于Mycase。

请如何构建这个,以便我可以通过mycase.users或mycase.admin等方式让用户... ????

1 个答案:

答案 0 :(得分:0)

a = [True, False, False, True, True, False, False, True]
b = [1,    2,     2,     1,    1,    3,     3,     2   ]

#Edit: the next two lines were originally and wrongly inside the for loop
moving=True
istart=1
for i,trp in enumerate((a)):
    if trp==False:
        if moving==False:
            # if this condition holds, the particle just started a new move
            istart = i
            moving = True
    else:
        if moving==True:
            # is this condition holds, the particle has stopped its move
            moving = False
            if b[i]==b[istart-1]:
                # if this holds, a needs to be adjusted
                a[istart:i]=[True]*(i-istart)

我将user_id添加到mycase,首先将其与user_id首先关联,并将所有其他typed_user正确关联,并将通知发送给他们。