1

I am having problems with the Rails runner. When I try to use if, even by command line, it does nothing! it doesn't show error messages, nor results from actions.

For example, if I try

rails runner Credit.count

having defined the model Credit, and the method count as

Credit.first.update_attribute(:estado, "En proceso")

or even simpler tasks, the runner does nothing!

I've tried saying that the environment is development, but nothing works. Does anyone has any insights? Am I doing something wrong?

Edit: I am watching the database, that's the problem, there is no update. I changed the value of the column "estado" for the first element to something other than "En proceso", however, no matter how many times I use the runner, the db doesn't show any change at all.

Soviut
  • 88,194
  • 49
  • 192
  • 260

3 Answers3

2

rails runner: runner runs Ruby code in the context of Rails non-interactively

update_attribute : it only return true or false, if you want to output the result, you can use "puts" or "p".

For example,

$ rails r "User.first" #no output, even it will return a user object
$ rails r "puts User.first" # you can use "puts" get the output
#<User:0x007f8a2c76e608>
pangpang
  • 8,581
  • 11
  • 60
  • 96
0

if you just written Credit.first.update_attribute(:estado, "En proceso") in console, you shouldn't have results, but you could see result in DB, for checking if update_attribute really update it you could write as update_attribute! also you could just write:

p Credit.first.update_attribute(:estado, "En proceso") which should print true or false to console

Igor Guzak
  • 2,155
  • 1
  • 13
  • 20
0

Turns out I am an idiot.

First, I had to change the name of the method, calling Credit.count calls the default count method, so I changed it to myMethod.

After that, I was able to use puts function and update the database easily :)