Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-单用户多“角色?”_Ruby On Rails - Fatal编程技术网

Ruby on rails Rails-单用户多“角色?”

Ruby on rails Rails-单用户多“角色?”,ruby-on-rails,Ruby On Rails,具有属于用户的发票模型和将用户关联为特定角色或多个角色的用户表: class CreateInvoices < ActiveRecord::Migration def self.up create_table :invoices do |t| t.string :po_number t.datetime :invoice_date t.datetime :date_received t.datetime :date_approved t.text :clerk_note

具有属于用户的发票模型和将用户关联为特定角色或多个角色的用户表:

class CreateInvoices < ActiveRecord::Migration
def self.up
create_table :invoices do |t|
  t.string :po_number
  t.datetime :invoice_date
  t.datetime :date_received
  t.datetime :date_approved
  t.text :clerk_note
  t.integer :clerk_id
  t.integer :approver_id
  t.text :approver_note
end
end

class Invoice < ActiveRecord::Base
  belongs_to :user  
end

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :first_name
      t.string :last_name
      t.string :username
      t.string :email
      t.boolean :account_clerk
      t.boolean :approver
      t.boolean :admin
    end
  end

class User < ActiveRecord::Base  
  has_many :invoices
end
在发票记录中,如何根据用户模型中设置的角色分配职员id和审批人id?每张发票都有一个职员和审批人,但都是用户

同样,如何将职员注释分配给职员id,将审批人注释分配给审批人id?我假设在这种情况下,我可以引用当前的用户,因为登录的用户将是做笔记的用户,对吗


我不知道这叫什么,所以在我的谷歌搜索中,我不知道该找什么。。。提前感谢你的帮助

要使用当前模型回答您的原始问题,您只需创建视图,根据用户的角色显示不同的字段。假设登录用户位于名为@user的实例变量中

<% if (@user.approver) then %>
   <%= f.text_area :approver_note %>
<% else %>
   <%= f.text_area :clerk_note %>
<% end %>

实际上,您可能希望从发票表中拆分“职员”和“审批人”列,并创建一个包含以下列的“发票用户”表:发票id、用户id、备注、角色。不确定同一用户是否可以同时作为系统中的审批人和办事员。感谢您的想法-是的,用户可以同时作为审批人/办事员中的一个或两个。我可以用这种方法。。。谢谢
if (@user.approver) then
  @invoice.approver_id = @user.id
else
  @invoice.clerk_id = @user.id
end