28

Here is my rake task

namespace :users do
  task :change_role, [:role] => :environment do |t, args|
    puts args.role
  end
end

I am calling it like this:

rake users:change_role["role"] but I am getting this error no matches found: users:change_role["role"]

4
  • Where are you putting this? There's nothing wrong with what you've written, or how you're invoking it.
    – user229044
    Jul 3, 2014 at 15:13
  • does your rake task file has file extension as .rake?
    – nickcen
    Jul 3, 2014 at 15:26
  • @nickcen - my file is named users.rake Jul 3, 2014 at 15:51
  • @meagar - it's in app/lib/tasks Jul 3, 2014 at 15:59

4 Answers 4

61

You need to escape the square brackets when using them in some shells like zsh:

rake users:change_role\["role"\]
6
  • No it is not. That is the error from your shell telling you there are no matches for that command found. With zsh and no escaping the output is zsh: no matches found: users:change_role[ok]. Using zsh with escaping the output is ok
    – infused
    Jul 3, 2014 at 16:39
  • You're correct, its rake saying it can't find the command. In any case, you need to escape the brackets with zsh.
    – infused
    Jul 3, 2014 at 16:42
  • Ah, actually I think you're right. "No matches found:" is definitely not being output by rake; rake would have said "don't know how to build task...".
    – user229044
    Jul 3, 2014 at 16:44
  • That escaping isn't correct... I tried this rake users:change_roles[\"hello\"] and I am still getting the same error Jul 7, 2014 at 12:58
  • 3
    Great call! the full error is indeed: zsh: no matches found: Jun 14, 2020 at 22:38
30

Put the rake task in single quotes.

rake 'users:change_role["role"]'

more on https://thoughtbot.com/blog/how-to-use-arguments-in-a-rake-task

3
  • The only thing that works for me (Rails 5.0.7.2)!! Feb 10, 2020 at 12:44
  • 3
    This is a zsh prereq.
    – Trip
    Feb 24, 2021 at 20:19
  • we should ask the OP if he is using zhs or not. But this helped me, as I just switched to zhs.
    – Dung Tran
    Sep 2, 2021 at 11:15
9

You can add unsetopt nomatch to your .zshrc file, as Chad Pytel describes in here

1
  • 1
    You the man, dog. This is the right answer for most people, especially those that are coming from bash. Jan 20, 2021 at 21:24
1

@infused way works, but if you want change to be permanent, so you can simply call rake users:change_roles["hello"], add following to your .zshrc:

alias rake='noglob rake'
1
  • this is working fine with me on Ubuntu 20.4, thank you
    – Astm
    Feb 11, 2022 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.