Posts Tagged ‘Ruby on Rails’

27th October
2011
written by simplelight

Great post on how to install Memcached from source and monitor its performance.

For a good tutorial on getting started with Memcached in a Ruby on Rails environment, checkout the Scaling Rails series, particularly episode 8

The Memcached wiki also has a page listing the various tools for monitoring Memcached.

 

19th September
2011
written by simplelight

I used Ubuntu 10.04 so that I know I don’t need to upgrade for the next four years.

1. Follow Linode’s excellent ‘Getting Started‘ instructions.

2. Add a new user and add them to the sudoers file.

3. Use Josh’s ‘Railsready‘ script to install Ruby etc.

Rather than using RVM to create gemsets, I prefer to ‘Vendor Everything‘, so I didn’t use RVM to install Ruby.

4. Install Passenger (this will also install nginx)

7th December
2010
written by simplelight

Is there any way to specify the RAILS_ENV when using Passenger? I tried setting it in my environment.rb, but it doesn’t seem to take anything other than “production” … setting the RAILS_ENV constant instead of the ENV['RAILS_ENV'] eventually did the trick.

For a way to set Rails_env differently depending on the capistrano stage being deployed to, see this post

25th August
2010
written by simplelight

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
4th April
2010
written by simplelight

Assuming you want to make sure that no two models have the same COMBINATION of values for a and b:

so having two models with:

a = 1, b = 2
a = 1, b = 3

would not be a conflict.

If that’s the case then the standard validation

class Widget < ActiveRecord::Base
validates_uniqueness_of :a, :b
end

wouldn’t work since it tries to prevent saving two models with the same value of a, OR with the same value of b.

And even if that’s not what you’re trying to do, and you’re ok with the example being a conflict, validates_uniqueness_of doesn’t guarantee uniqueness if two users try to save conflicting records simultaneously.  The validation works by first trying to find a record with the value, and if it doesn’t find it inserting the ‘new’ record, and this can fail due to a concurrency hole.

To fill this hole requires leaning on the database server, and the way to do that in SQL is by having a unique index on the table which covers the column or columns you want to be unique. This assume you are using a database which supports it, e.g. MySql.

To create an index you can create a migration which includes a statement like

add_index  :widgets, [:a, :b], :unique => true)

Assuming that the table name for the model is ‘widgets’

Now if you do this, you also need to be aware that if you try to save a record with a uniqueness conflict the save will raise an ActiveRecord::StatementInvalid exception, which you’ll need to rescue and do something like telling the user of the conflict so that he can rectify it.

2nd December
2009
written by simplelight

>> ActiveRecord::Base.connection.instance_variable_set :@logger, Logger.new(STDOUT)

5th July
2009
written by simplelight

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:

  1. Install the gem: sudo gem install cldwalker-hirb –source http://gems.github.com
  2. Start the console: ruby script/console
  3. Require Hirb: require ‘hirb’
  4. Enable it: Hirb.enable
  5. Try it: x = Model.find(:all)
2nd April
2009
written by simplelight

I was using the Rails helper observe_field to monitor a text entry form on my web app. I couldn’t figure out why the relevant method was not being called until I realized that it was happening every time there was a single quote in the text entry. The solution is simple. Use the escape_javascript function as shown below:

[ruby]
<%= observe_field :guess,
:frequency => 0.5,
:update => 'ajaxWrapper',
:url => {:action => 'feedback', :o nly_path => false},
:with => "'correct=#{escape_javascript(@text)}&guess='+encodeURIComponent(value)" %>
[/ruby]

8th February
2009
written by simplelight

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:

  1. Download and unzip Bort into project folder
  2. Edit the database.yml and the settings.yml files
  3. Create the database: mysqladmin -u <username> -p create <db_name_development>
  4. If you’re using Dreamhost: add ‘host: mysql.<dbhostname>.com’ to your database.yml file (in the production environment)
  5. rake db:migrate
7th February
2009
written by simplelight

The following gems and plugins are the most popular as of Nov 12th, 2008:

  • Javascript Framework: jQuery (56%), Prototype (43%)
  • Skeleton: Bort
  • Mocking: Mocha
  • Exception Notification: Hoptoad
  • Full text search: Thinking Sphinx
  • Uploading: Paperclip
  • User authentication: Restful_authentication (keep an eye on Authlogic)
  • HTML/XML Parsing: Hpricot
  • View Templates: Haml

NewRelic has a good article on the state of the Rails stack.

Previous