0

I have a Command table which look like this in rails console

 Command(id: integer, created_at: datetime, updated_at: datetime, timeout: 
         integer, name: string, command: text, action: string, reboot_action:
         string, num_retries: integer, partnum: string, path: string,
         firmware: string)

The last command in the table look like this:

<Command id: 1, created_at: "2017-02-09 17:44:10", 
         updated_at: "2017-02-09 17:44:10", timeout: 60, 
         name: "test", command: "home/app", 
         action: "up">

So how to pass the command object to the method, So that i will have the required information about what command to run and how to handle a failure.

2 Answers2

0

You would pass it as a variable if the method is on another object. If you want to call a method on your command object, the you already have the info needed in the instance.

def some_method(command)
  #do stuff with command
end

command = Command.find 1486662250106112 #get your command instance somewhere
some_method(command) #passing the command instance to the method
Sean
  • 983
  • 5
  • 13
0

If you're trying to invoke the shell command in the command record's command column, (Sidenote: You might want to rethink some of these names, as the name "command" can refer to a lot of things here...) you can do so as follows:

command = Command.last  # get the command
system(command.command) # invoke the command

In fact, there are several ways to call a shell command in ruby and this is only one of the more common ways of doing so. See this answer for more details on shell commands in ruby, and for alternative methods for doing this.

Finally, if you're worried about sanitizing your input, (i.e. if you're worried about users inserting custom, malicious code) then take a look at the documentation for Shellwords.

Glyoko
  • 2,071
  • 1
  • 14
  • 28