Rails traps into which I fall every damn time

  • If the return value of a method in your tests is totally different from what you see in the source code, chances are 100:1 that you (or your colleague) stubbed it out before the test run
  • You can’t set the sequence of validations in ActiveRecord. You just can’t. Don’t add validations that depend on each other, e.g. ensuring the presence of X in one validation and in another that X > Y.
  • Be carefull where you use symbols. Something like
    belongs_to :preferred_city, :class_name => City, :foreign_key => :city_id
    

    will throw something like

    NoMethodError: undefined method `match’ for #

    because you must use strings here a la:

    belongs_to :preferred_city, :class_name => 'City', :foreign_key => 'city_id'
    
  • If you have trouble saving a model via web interface for no obvious reason double check the log, there’s a good chance that you forgot to use attr_accessble
  • Similar to above: If you use nested forms e.g. User has_one profile and your editing user and profile in one form don’t forget
    attr_accessible :profile_attributes
    

    in your user model

  • When manually constructing links like:
     def my_url
        "/offer/#{city.to_param}"
     end
    

    don’t forget the leading slash unless you really want a relative path.

  • Formtastic is great, but I always forget that it’s
    form.input :field, :input_html => ...
    

    and not

    form.input :field, :html => ...
    

    and it’s

    form.button :commit, :button_html => ...
    

    and not

    form.button :commit, :html => ...
    
  • I constantly forget to interpolate strings in regexes, so given
    pattern = 'oh no'
    

    I find myself writing

    if input =~ /pattern/
    

    instead of

    if input =~ /#{pattern}/
    
  • When you pass numeric values to cucumber steps make sure to call to_i, to_f or whatever is appropriate because cucumber passes everything as a string (yes, an easy one, but I forget it every now and then)

About troessner

I am a Ruby enthusiast currently working for dealvertise in Berlin. I like dynamic languages in general with a strong focus on Ruby. Headhunters, don't bother. No, I do NOT want to be a Java architect for a mid-size company in the middle of nowhere. I probably would love Python if it wouldn't force me to write `self` all over the goddamn place. I really suck at functional programming. I should have paid more attention at university. NoSQL is worth the hype. Or is it? I am a big advocate of Behavior Driven Development. Yes, really. I tried to love node.js, but in the end, it's still fucking javascript. I was a devoted SCRUM believer. I have a deep interest in Erlang but haven't had a chance to really use it. I try to contribute as much as possible to open source. Right now I am the maintainer of the transitions gem (https://github.com/troessner/transitions), the ecircle gem (https://github.com/troessner/ecircle) and the has_uuid gem (https://github.com/troessner/has_uuid). Besides that I recently got fascinated with Rubinius and I am trying to really understand why this works. Furthermore, I have made some rather petty contributions to Rubinius: https://github.com/rubinius/rubinius/commits/master?author=troessner You can find most of the code I write on github - https://github.com/troessner Sometimes I write down stuff on: http://troessner.wordpress.com/
This entry was posted in rails, Ruby. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s