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?
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?"
Related
Here is a question.
I have an Rails project. When I want to clear my database and fill it with some test data I run:
rake db:drop db:create db:migrate db:seed
and I have an error:
NoMethodError: undefined method login for #<User:0x007fecf46afe80>
When I run separately:
rake db:drop db:create db:migrate
rake db:seed
all goes fine.
Also all my actions in db/seeds.rb are wrapped in ActiveRecord::Base.transaction block.
I had to add User.reset_column_information in the top of my db/seeds.rb to make
rake db:drop db:create db:migrate db:seed
working.
I didn't have the same errors before without reset_column_information. Does somebody have any ideas why it's happen?
PS: after running rake db:drop db:create db:migrate there is "missing" column in db/schema.rb and I can see this column in DB directly
From the docs:
reset_column_information() public
Resets all the cached information
about columns, which will cause them to be reloaded on the next
request.
The most common usage pattern for this method is probably in a
migration, when just after creating a table you want to populate it
with some default values
You're db:migrate task changes the column information in the users table, but it looks like these changes are not properly written to the db/schema.rb file until the rake cammand finishes. Your db:seed task looks at db/schema.rb to see if the columns it needs exists, but this shows the schema as it was before the db:migrate unless you put it in a separate rake command or you run reset_column_information() before it.
I am trying to create a database using:
rake db:create
That output a error: db already exists.
When I do a
rake db:drop
The output error is: db does not exist.
I have use external to connect to the DB server and indeed the DB does not exist.
Any help is much appreciated.
Every time I run the command:
rake test test/models/post_test.rb test_the_truth
I get the following error:
`initialize': FATAL: database "knome_test" does not exist (PG::Error).
Can someone explain and help me to debug it?
It sounds like you have not created the database.
Try running the following to create, migrate, then prepare your test database.
rake db:create
rake db:migrate
rake db:test:prepare
You have create a new database in postgres and update database.yml "test" section with that db_name, username & password. The same way you have done in "development" section.
On my local machine I can do
QUEUES=a,b,c,d rake resque:work
And it processes those queues in that order. However, on Heroku Cedar I add this to my procfile:
worker: QUEUES=a,b,c,d exec bundle exec rake resque:work
And it crashes the app on deploy. I'm probably missing something dumb, but I'm stumped.
PS I prefix the command with exec because of a bug with resque not properly decrementing the worker count.
You shouldn't need the initial exec. The entry should look like this:
worker: bundle exec rake resque:work QUEUE=a,b,c,d
Use #hone's fork to properly clean up workers when they quit. In your Gemfile:
gem 'resque', git: 'https://github.com/hone/resque.git', branch: 'heroku', require: 'resque/server'
How do i pass jvm arguments like Xmx to a rake task in jruby?
Am using rvm and running the rake task "rake db:migrate".
Thanks!
Rather that put the entire path, if jruby is already the Ruby you're using (for example, with rvm) such that just typing rake would be using jruby, you can use a double dash to send the arguments to the jvm.
Examples:
bundle exec rake -- -J-Xmx1024m (if you're using bundler)
rake -- -J-Xmx1024m
Found it!
jruby -J-Xmx2048m -S /home/prats/.rvm/gems/jruby-1.6.1#myapp/bin/rake db:migrate