jemnotesversion 2 / featuring rails or see all/search

Apr 27
I have now several times had hard-to-debug problems that came down to field size limits. When developing with sqlite, it’s easy to forget about field limits. When deploying to mysql, these come into play. Don’t forget about them!
Apr 11
From here comes a nice way of raising 404 errors in rails.
Inside application_controller.rb:
class Error404 < StandardError
end

class ApplicationController < ActionController::Base
  rescue_from Error404, with: :render_404

  def render_404
    respond_to do |format|
      format.html{render file: "#{RAILS_ROOT}/public/404.html", status: 404}
      format.all{render nothing: true, status: 404}
    end
    true
  end

  # etc.
end
Then, inside the relevant controller:
raise Error404
Also, as explained on the above website, the last line true in render_404 means you can
render_404 and return
Apr 11
I was trying to figure out why protect_from_forgery didn’t seem to be working in rails. I was trying to get rails to fail by changing the authenticity_token passed to the server during ajax requests. However, I had forgotten that XMLHttpRequests are subject to the same origin policy (SOP), so, for ajax, you don’t need a valid authenticity token to be sure the request is ok.
I once learnt all this, but forgot when trying to protect my application.
Apr 7
A few notes for rails:
  • If you are using serialize, you can’t use the self[:attr] form. You must use self.attr. If necessary, use alias to make this happen.
  • rake stats gives some interesting statistics about your rails project.
  • Instead of read_attribute :name you can just use self[:name]. Similarly for write_attribute and []=.
Mar 27
In rails, instead of read_attribute(:attr_name) you can just use self[:attr_name]. Similarly for write_attribute.
Mar 6
Suppose you have this:
class Widget < ActiveRecord::Base
  def errors
    [1,2]
  end
end
where errors is a supposedly harmless, unrelated method that returns errors related to your Widgets. Then, try the following in your controller:
wdgt = Widget.new
wdgt.save!
and you will get a message saying something like
NoMethodError (undefined method `full_messages' for #<Array:0x00000102344c68>)
This is because rails tries to print out wdgt.errors.full_messages, and fails to find full_messages on the array [1,2] returned by your custom errors method.
Thus, my rails hint: don’t create an errors method in an activerecord model.
Feb 1
The syntax for serialize in rails is
serialize attr_name, class_name=Object
thus, you can’t chain serialize :attr_1, :attr_2 like you can with attr_reader :attr_1, :attr_2—you’ll get some bad errors.
Feb 1
From isitruby19.com, to get mongrel working with rails, just do this:
gem install mongrel --source http://gems.rubyinstaller.org
Feb 1
It seems that getting multi-threaded processes working nicely in rails is quite hard. You can’t just use a ruby thread—you’ll have all sorts of problems with classes being unloaded behind your back, and weird failures for models to be adjusted in the background. Here are some pages about it:
  • Threading in rails, an article from August 2007 which describes some problems the author was having, which sound very similar to mine.
  • A new version of the above article from February 2009, which talks about some of the problems he had with the first approach.
  • Spawn, which describes the module I ended up using.
  • BackgrounDRb, which has awful capitalization in its name, but is an alternative approach for running background jobs. This moves it outside the main process, and is more involved an approach than I wanted.
  • delayed_job, an alternative to BackgrounDRb.
Good luck getting threading working how you want it in rails. In my case, I’ve installed the spawn plugin using
script/plugin install git://github.com/tra/spawn.git
and then I have
spawn do
  # job running in the background
end
which seems to be working for now. Phew.
Jan 26
Problem when starting rails:
<blah blah blah>: no such file to load -- net/https (RuntimeError)
Solution: install libopenssl-ruby.
Nov 28
I couldn’t get script/console to load the right version of Ruby. Turns out you can use
script/console --irb=irb1.9
May 25
May 18
A nice page explaining how to set up .gitignore rules for rails apps.