Run multiple command lines in ruby

Viewed 426

I need to run multiple Ruby commands inside docker. What I normally do from SSH is:

docker exec -it containername bundle exec rails c

And then run my command. In this case:

SharedAccess.create(room_id: 56, user_id: 2)

The thing is I need to run multiple commands with different user_id's.

SharedAccess.create(room_id: 56, user_id: 3)
SharedAccess.create(room_id: 56, user_id: 4)
SharedAccess.create(room_id: 56, user_id: 5)

What I normally would do in shell is creating a script, pasting all the lines one below the other in there and then just run it. I want to find a way to create something like a script with all the lines in there and then run it from rake (inside docker).

1 Answers

create a file somefile.rake in your lib/tasks directory:

namespace :migrate do
  desc "create some SharedAccess"
  task shared_access: :environment do
    SharedAccess.create(room_id: 56, user_id: 3)
    SharedAccess.create(room_id: 56, user_id: 4)
    SharedAccess.create(room_id: 56, user_id: 5)
  end
end

and now you can run docker exec containername bundle exec rake migrate:shared_access

Related