Now Playing Tracks

validates_presence_of saves NULL as valid

“Model w/string :url, validates_presence_of :url but saves NULL as valid, then won’t update via any method.”

This is the description of an actual error I was asked about. And a tricky one, too. would complain that the url is missing until you called

photo.url = ""

That got it to save the Photo record without complaint, but the url column still was nil when the record got reloaded.

The culprit it turns out was this little typo:

class Photo attr_accessor :url end

where it should have been

attr_accessible :url

Easy to get wrong and easy to miss, this one actually overwrites the url method that ActiveRecord generates with a plain old accessor, but the validation still validates the method and its value properly. The value just doesn’t end up in the attributes hash anymore and therefore doesn’t get persisted.

The devil is in the details.

Be careful with git commit

It’s not like you can permanently break stuff with

git commit -am "message" && git push origin

That doesn’t mean it’s a good idea to do it. You - like me - might end up with embarrassing pieces of git history like:

made sure the right files are precompiled and loaded Revert “Fix because none of those files don’T even exist anymore” Fix because none of those files don’T even exist anymore

Don’t do that. People might laugh at you once they look into what happened there because they see that you pushed your local machine’s configuration to the repo…

js has already been required

Today I came across this one:

    application.js has already been required

when compiling the layouts of an app. According to the stack trace it happend here

    <%= javascript_include_tag 'application' %>

but commenting this out removed the js file altogether.

Eventually I figured out that because of this line in application.js

    //= require_tree .

the asset pipeline required a file left over from a merge called application.js.orig. Removing that file solved the problem.


If you see this error and are sure that this file is only required once, check for duplicates left from merge conflicts or files with an additional preprocessor extension like .js.erb or similar.

No a++ in Ruby?

Today I’ve heard (again) that Ruby doesn’t have the a++ construct so common to many other programming languages (or an a-- for that matter). While that’s true of course, let me point out these two nice methods:

Integer#succ and Integer#pred

If you dislike having to write u + 1 every time you want to increment a number, try u.succ instead! I’ve got to mention though, that it’s not exactly the same as x++ because succ doesn’t replace the variable. You would still have to write x = x.succ because numbers can’t modify themselves like Strings do.

So do also take a look at String#succ. It can do really cool things.

To Tumblr, Love Pixel Union