Posts Tagged ‘rails’
When you’re debugging/analyzing MySQL queries in the Rails console, it helps to turn on ActiveRecord logging:
#Enable ActiveRecord logging def loud_logger(enable = true) logger = (enable == true ? Logger.new(STDOUT) : nil) ActiveRecord::Base.logger = logger ActiveRecord::Base.clear_active_connections! end
collect {|item|block}
and map{|item|block}
do the same thing – return an array of things returned by the block. This is different from returning specific items in the collection being iterated over.
Which leads to select
.
select{|item|block}
will return actual collection items being iterated over if, for each item, the block condition evaluates to true
. Not the same as returning what the block, itself, may return. In the case of select, the block would always return an instance of class TrueClass
or FalseClass
. Typically, [true, false, ..., true]
is not what you’re looking for in your resulting array.
Slightly modifying the core RDoc example:
a = ["a", "b", "c", "d"]Â Â Â Â #=> ["a", "b", "c", "d"]
a.map {|item|"a" == item}Â Â Â #=> [true, false, false, false]
a.select {|item|"a" == item}Â #=> ["a"]
If you’re trying to run the rake task required for installing Cucumber in Rails and keep getting the following error message:
rake aborted!
undefined method `select’ for class `ActiveRecord::ConnectionAdapters::MysqlAdapter’
Check to see whether you’re using the query_analyzer plugin. I had to uninstall it to get the rake task to complete using:
script/plugin remove query_analyzer
That solved the problem
I use the Rails console mainly to poke around in my database. Unfortunately the display of the records returned leaves a lot to be desired. Hirb solves this problem perfectly! Here are the quick steps you need to get the basic functionaliy:
- Install the gem: sudo gem install cldwalker-hirb –source http://gems.github.com
- Start the console: ruby script/console
- Require Hirb: require ‘hirb’
- Enable it: Hirb.enable
- Try it: x = Model.find(:all)
I highly recommend Bort when you’re starting a new Ruby on Rails project. Assuming you’re using MySQL, the following steps are all that are necessary to get started:
- Download and unzip Bort into project folder
- Edit the database.yml and the settings.yml files
- Create the database: mysqladmin -u <username> -p create <db_name_development>
- If you’re using Dreamhost: add ‘host: mysql.<dbhostname>.com’ to your database.yml file (in the production environment)
- rake db:migrate
Unfortunately, the New Relic performance monitor for Ruby on Rails doesn’t work with mod_rails (Passenger). According to a support email from them it currently “only supports mongrel and thin (without sockets)”. They plan to support Passenger in the future. That’s great news because they provide an excellent performance monitoring tool which is very easy to install and use.
Update: New Relic has added support for mod_rails. I received this email from their excellent customer support:
I was just digging through my support emails and found a few people who had inquired about RPM supporting Phusion Passenger, aka mod_rails.
Â
I wanted to let you know that we released a version of the agent with ‘beta’ support for Passenger.
Â
If you’re interested, check it out and let us know how it works for you!
Â
To try it out, just do:
Â
script/install –force http://svn.newrelic.com/rpm/agent/newrelic_rpm
Â
Bill Kayser
New Relic RPM Developer
Â
If you’re running your low-volume Ruby on Rails app on mod_rails (Passenger) and have wondered why the first page takes 5 seconds or more to load, there is an excellent explanation here.
Over Memorial Day weekend I migrated my Rails Application to Dreamhost using mod_rails (Passenger). It was not an entirely smooth process but I was also upgrading from Rails 1.8.x at the same time. That was compounded by making the foolish mistake of trying to rebuild my database using Rake migrations. (That’s a bad idea. I could have saved many hours by just uploading the schema)
Here is the procedure I followed (hat tip to Nock):
- cd ~/
- rails your_app_name -d mysql
- Copy app/, database.yml, routes.rb, db/
- Change public/.htaccess from .cgi to .fcgi
- put your app into production mode (uncomment line 5 in environment.rb)
- run rake db:migrate RAILS_ENV=production
- chmod -R 755 ~/your_app_name/app
- rm your_app_name/public/index.html
- killall -USR1 dispatch.fcgi
- killall -USR1 ruby
One comment on step 4. For some reason none of my stylesheets would load. Much of the advice gleaned from endless Google searches seemed to suggest that the problem would be fixed by setting the RewriteBase in /public/.htaccess. That turned out to not be the case.
My stylesheet problem was caused by having this line twice in my .htaccess file
RewriteRule ^(.*)$ dispatch.fcgi[QSA,L]
DO NOT uncomment the one before RewriteEngine On , as all the tutorials seem to imply, just change the .cgi to .fcgi in the block below it.
Thanks to Dreamhost for their stellar support over a frustrating (for me!) Memorial Day weekend. In the end, (as is so often the case), very little of the frustration was caused by Dreamhost or mod_rails but, rather, by some of the vagaries of Rails. I’m guessing that future deployments would be much smoother as this was my first time deploying to a shared hosting environment.
I use Dreamhost to host my websites and they have now added support for Passenger (a.k.a mod_rails). Ruby on Rails deployment hassles should be a distant memory soon!
If you’re looking for a cheap and cheerful hosting company for your Rails app, I highly recommend Dreamhost. It’s great for the solo developer (or small team) because for a small amount per year you can launch your site on a shared hosting service and then later easily migrate it to a virtual private server as your needs change.Â