使用子级条件对.each方法轨的返回结果进行细分

时间:2019-01-15 12:10:06

标签: ruby-on-rails ruby

我有一个属于document.rb的权限表,而文档属于workstream.rb

<% @permissions.each do |p| %>
  <% if current_user.workstream === p.document.workstream %>
     # all the values that are true to this condition should be captured in an array or .where sort of return array           
  <% end  %>
<% end %>

请我如何捕获数组中满足if条件的值并计算值。

因此将当前用户工作流中具有相同文档的所有权限存储在数组中并计数

class Permission < ApplicationRecord
 extend FriendlyId
 friendly_id :id, use: :slugged
 belongs_to :user
 belongs_to :document
 enum status: [:request, :approved, :rejected]
end   

class Document < ApplicationRecord
 extend FriendlyId
 friendly_id :doc_uuid, use: :slugged
 mount_uploader :doc_resource_url, DocumentUploader
 before_save :generate_token
 belongs_to :workstream
 has_one :document_meta
 belongs_to :state
 has_many :permissions
end

class Workstream < ApplicationRecord
  extend FriendlyId
  friendly_id :fullname, use: :slugged
  belongs_to :unit
  belongs_to :state
  belongs_to :project
  has_many :documents
  has_many :user_workstreams
  has_many :users, through: :user_workstreams
  validates_uniqueness_of :workstream_code
end

错误消息

2.4.0 :026 > Permission.joins(document: {workstream: {id: 1}})
ActiveRecord::ConfigurationError: 1
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:70:in `walk_tree'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:67:in `block in walk_tree'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:65:in `each'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:65:in `walk_tree'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:67:in `block in walk_tree'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:65:in `each'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:65:in `walk_tree'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:67:in `block in walk_tree'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:65:in `each'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:65:in `walk_tree'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:62:in `block in walk_tree'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:61:in `each'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:61:in `walk_tree'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:52:in `make_tree'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/associations/join_dependency.rb:97:in `initialize'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activerecord-5.0.7/lib/active_record/relation/query_methods.rb:1071:in `new'
... 9 levels...
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/railties-5.0.7/lib/rails/commands/console_helper.rb:9:in `start'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:78:in `console'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/railties-5.0.7/lib/rails/commands.rb:18:in `<top (required)>'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:293:in `require'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:293:in `block in require'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:293:in `require'
    from /Users/chineduabalogu/work/HSDF_DHUB/bin/rails:9:in `<top (required)>'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:287:in `load'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:287:in `block in load'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /Users/chineduabalogu/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:287:in `load'
    from /Users/chineduabalogu/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/chineduabalogu/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

0 个答案:

没有答案