Main

Ruby on Rails Archives

January 14, 2012

Rails tests gotcha with I18n.locale

A example of a bad I18n rails test... Found some tests failing in a project because tests in different files would change I18n.locale, changes like this make your tests fail if the tests are not run in the standard order. since this took me awhile to track down, I thought I should share a quick example for others to find. So if you are having a issue like this remember to add a teardown method that resets I18n.locale to the default local.

November 16, 2011

ActiveRecord / Rails case insensitive ordering

It is pretty common to want to have the results of a query sorted by order. I wanted the results sorted by name on a user object. Oddly it is hard to find how to make a string based order case insensitive on google. So for other having the same issue, it is simple, just add lower(field) in the order clause and it will sort in a case insensitive manner. Now I don't care when a user doesn't capitalize the first letter of their name.


User.order('lower(name)').all

November 14, 2011

Announcing NothingCalendar

I wanted a small project to play around with. A project that was small and simple that I could actually get done in my free time. An app I would use myself. I ended up deciding to build NothingCalendar, which was inspired by CalendarAboutNothing. It let's users track events, anything they feel like tracking over time. Mark off days you worked out, kept to your diet, worked on writing, studied, quit smoking, or anything else.

The idea was a simple way that I could add marks on a calendar on my phone, and have it sync-able and shareable. I also wanted it to be useable when you are offline, since phones loose connection often in metros, planes, lines, etc.

One goal with the project is to keep it is small enough that I can build it and play around with different ideas. It is such a small app, that I could rewrite the entire thing with a new methodology if I wanted. Which is exactly what I plan on starting to do. I wanted a app that I could practice new skills and techniques on, while still being useful, and something I would use on a day to day basis.

I started working on the project with the idea to build up a chain of doing some open source experimental development having a playground to try things and learn. A small app where I could push myself to look at new technologies, and to force myself to release them into a production environment, opposed to another one off script or tests that never see the light of day. I highly recommend forcing yourself to do a bit of development every day, it is recommended by a bunch of smart people (Andrew Hunt and David Thomas's The Pragmatic Programmer: From Journeyman to Master, Chad Folwer's The Passionate Programmer, Paul Graham's Hackers and Painters). I was in part inspired by Chris Strom (@eee_c), who has done incredible work keeping up a chain and blogging about what he learns.

NothingCalendar is a testament to what you can do in about 15 minutes a day. That is about how much time I have spent on it most evenings. Just forcing myself to do a bit every day, has led to my longest streak on CalendarAboutNothing (66 days at the moment). Just remembering to code a bit everyday, keeps the project in your mind, keeps you improving it. It also makes refactoring little things a perfect task, you only have a few minutes take something on. Complete a tiny refactoring of from a todo list and check it in. I frequently just check in #TODO comments, and grep for them when I don't have much time or an idea of what to work on. After a tiny todo, call it a day and know you will have more time sometime soon to do something more substantial.

Please give NothingCalendar a try, and let me know how it goes or any problems / feature ideas. I will will be working more more on it each day, and trying to rewrite large parts of the app using some new ideas.

October 2, 2011

ActiveRecord Association objects can act weird (bug in association#any?)

I found a weird issue when working with ActiveRecord AssociationProxy objects, they weren't proxying the method correctly.

The issue arose when trying to use the #any? method on an array which should be true when empty. When calling this on a AssociationProxy object that has 0 items it was returning false. Oddly enough inspecting or to_s the association and then calling #any? resulted in the correct behavior. It was so weird though that after looking at other options I ended up using the present?, which always behaved how I expected it to. Even weirder is the issue only presented itself in the rails server debug and production modes. It always acted as one would expect in rails console. I started to dig in more, but ended up yak shaving a lot of other stuff to try to learn exactly what the cause was. If anyone understands the issue better, I would be happy to know more.

Until then, be aware that at least in the 2.3.X versions of rails, ActiveRecord AssociationProxy#any? acts funky and should not be trusted. Use a work around like present? or length > 0 (both worked correctly in console and on dev/production server environments). This is one of these issues where there is so much going on in the rails stack that it is hard to determine where the issue lies.

So for those googling this issue, beware AssociationProxy.any? on has_many relationships.

Example code:

May 1, 2010

A archive of my last project

Since Caliper is shutting down on April 30th, I made a little video to show what it did, and explain it's purpose. It isn't really promotional, but if people ever want a reference of something I worked on this should help.

Caliper Screencast from dan mayer on Vimeo.

October 16, 2007

Ruby on Rails built in string shortcuts

There are some great string munipulation shortcuts in rails. I thought this was a good write up and little collection of some of the most useful functions.

Rails string shortcuts

September 7, 2007

Always use lenth on ruby collections

This bug never got me, but it is a good little ruby best practice to follow. So remember when ever using ruby collections use .length call not the .size call...

Why use length on all ruby collections.

August 12, 2007

Finished Rails Book

I finally finished a reading the rails book all the way through. I keep putting it off and reading other books because reading straight through a programming book is boring. Yippie for finishing that up, and I learn some more little rails tricks that should be helpful.

Next up Linux System Administration and Backup and Recovery!

July 31, 2007

Using Rails link_to_if

There are a lot of great helper methods in rails, and I need to remember to use more of them, one I over looked for a long time that I have found useful is link_to_if

<%= link_to_if logged_in?, "#{item.name}", :controller => 'items', :action => 'show', :id => item %>

replaces the much longer and more verbose:

<%- if logged_in? %>
<%= link_to , "#{item.name}", :controller => 'items', :action => 'show', :id => item %>
<%- else %>
<%= item.name %>
<%- end %>

I like being able to replace five confusing lines with one single simple solution. The link_to_if helper writes a link if the condition is met, otherwise it just outputs the text that would have been in the link. Very cool.

Also if you want to use Ajax forms with multiple submit buttons in Rails, here is the solution.

Lastly if you are trying to write a selector and want to make sure the test is returning the html you expect you can add puts @response.body into the test after the page call and see the response. This has been useful to me a few times.

June 26, 2007

Highly useful assert_select

I find myself using the assert_select a lot while doing testing and rails. I have had to keep looking up different examples depending on the situation. I have found that this page seems to have most everything I need when I can't remember it off the top of my head.

Rails assert select examples

July 10, 2006

Ruby on Rails Gem error could not find gem

I got this gem error on Ubuntu 6.06 over and over then finally I followed these instructions to install Ruby on Rails,

Install Ruby on Rails on Ubuntu 6.06

and I changed one line:

sudo gem update --system IS WRONG!!!!

run:

sudo gem update

and thing all the sudo gem installs work perfectly fine for me. Why I have no idea. I hate computers sometimes, but at least ruby and rails now work for Ubuntu 6.06 on my system.

Web 2.0 craziness

View Dan Mayer's profile on LinkedIn


I Power Seekler
I Power Seekler

www.flickr.com
This is a Flickr badge showing public photos and videos from mayer_dan. Make your own badge here.

Creative Commons License
This weblog is licensed under a Creative Commons License.