anteo / redmine_custom_workflows

Allows to create custom workflows for Redmine
http://www.redmine.org/plugins/custom-workflows
GNU General Public License v2.0
182 stars 72 forks source link

Help in error #195

Closed koren85 closed 4 years ago

koren85 commented 4 years ago

I get Error when I use this code in before_save:

c_role_ids = User.current.membership(self.tracker_id).try(:role_ids) || []
  u=User.find(self.assigned_to)
  a_role_ids = u.membership(self.tracker_id).try(:role_ids) || []
  if c_role_ids.include? 6
    if a_role_ids.include? 3 
      raise WorkflowError.new("Error")
    end
  end

Eror then I save rule:

undefined method `membership' for nil:NilClass``

but in ruby console I get rusult


irb(main):018:0> a_role_ids = u.membership(30).try(:role_ids) || []
=> [9, 3, 13, 19, 15]

What I do wrong?

AirTibu commented 4 years ago

Hi,

You get an error, because you're using an assigned user for membership and the interpreter also thinks of the possibility of what happens if the assigned user is not specified. In this case, you want to get membership for nil.

Try this code, it should work:


if self.assigned_to
   c_role_ids = User.current.membership(self.tracker_id).try(:role_ids) || []
   u=User.find(self.assigned_to)
   a_role_ids = u.membership(self.tracker_id).try(:role_ids) || []

      if c_role_ids.include? 6
         if a_role_ids.include? 3 
            raise WorkflowError.new("Error")
         end
     end
end

I hope its help!

koren85 commented 4 years ago

Hi, Yes, it helped me. Thank you so much!