1

I want to count the votes a user has given to an article and save it somewhere.

I want to check all the SQL INSERT or CREATE lines executed when we do something like:

>$ bundle exec rake db:reset
>$ bundle exec rake db:seed
>$ bundle exec rake test:prepare

Is there a way I can check the SQL commands in Ruby on Rails?

3
  • Someone downvoted my question without giving any reason why? :/ Commented Mar 21, 2016 at 16:06
  • Not clear to me either. Commented Mar 21, 2016 at 16:08
  • It's possibly been down voted because the question isn't asked well. Please read "How to Ask" and "minimal reproducible example". We expect to see evidence of your effort, in this case, what you tried and why it didn't work. You appear to be asking us to write it for you. Also, your question could have been written more clearly. Commented Mar 21, 2016 at 18:59

1 Answer 1

1

You can add a custom Rake task and use it whenever you need to log the SQL output:

task log: :environment do
  ActiveRecord::Base.logger = Logger.new(STDOUT)
end

Now you can run:

bundle exec rake log db:reset
bundle exec rake log db:seed
bundle exec rake log test:prepare

See "Is it possible to output the SQL change scripts that 'rake db:migrate' produces?"

Sign up to request clarification or add additional context in comments.

2 Comments

Could you tell me where I will define a custom rake task ?
lib/tasks/task_name.rake

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.