Posted: January 25th, 2010 | Author: abel | Filed under: Quick Hits, Rails | Tags: Sass | No Comments »
Just a quick note about sass. Let’s say you had the following in /public/stylesheets/sass/foo.sass:
body
line-height:1.2em
h1
color:red
This would render a blank document for /public/stylesheets/foo.css. You won’t get an error about it, you’ll just get a blank document. Here’s the thing, when you’re declaring a CSS property, you MUST put a space after the colon. Failing to do so will just wind up in parsing errors on the sass side.
I hope I’ve saved you some head scratching.
Posted: May 17th, 2009 | Author: abel | Filed under: Quick Hits, Rails, Uncategorized | 1 Comment »
(1) Before you unpack your gems, navigate to your vendors directory:
/Users/[yourname]/[your-app]/vendor/gems: gem unpack [gem name]
It isn’t the end of the world if you don’t do it ahead of time, but it does make life slightly easier.
(2) Remember that when you find a bug, you should write a test for the expected behavior BEFORE fixing the bug. “The Rails Way” has a great quote about testing:
“It’s not that Rails encourages you to do test-driven development, it’s that it makes it difficult for you not to do test-driven development.” ~Brian Eng
I’ve interpreted this quote into the following mantra:
“It’s not that rails makes it easy to test, it that it makes you pay when you don’t”.
Finding a bug after creating your test suite just means that you missed a test. Take the opportunity to correct that.
Happy hacking.
Posted: April 23rd, 2009 | Author: gabe | Filed under: Rails | Tags: active record, activerecord, caching, Rails, tips | 2 Comments »
ActiveRecord will cache the results of association method calls by default, unless you tell it not to.
(This applies to Rails 2.3.2 and perhaps earlier versions.)
From the documentation:
project.milestones # fetches milestones from the database
project.milestones.size # uses the milestone cache
project.milestones.empty? # uses the milestone cache
project.milestones(true).size # fetches milestones from the database
Normally, this is great. However, if you’re not aware of this default caching, you might see some strange behavior in your app or tests and have no idea what’s going on. This default caching behavior had me and Abel stumped until we read about it in the docs.
Working with the same concept as the example in the documentation, where a Project has many Milestones, here’s a more explicit example of the caching behavior in action:
project_1 = Project.find_by_id(1)
project_2 = Project.find_by_id(1)
# Load the same Project into two variables
project_1.milestones.length
# - Hits the db's Milestones table.
# - Caches milestones object on project_1.
# - Returns 0.
Milestone.create(:project_id => 1, :name => 'New Milestone')
# Adds a milestone to the project.
# But we don't do it through the project_1.milestones association
# because that _would_ update project_1.milestones's cached value
project_1.milestones.length
# Returns 0 (not 1, like you'd expect)
# because project_1.milestones was cached when
# previously requested above.
project_2.milestones.length
# Returns 1, because project_2.milestones
# hasn't been requested/cached yet.
# Note: project_2.milestones is
# a COMPLETELY DIFFERENT IN-MEMORY OBJECT
# than project_1.milestones.
# Taking this point further, here's an explicit example:
project_2.milestones << Milestone.create(:name => 'Another Milestone')
# Put another milestone on the project through
# project_2's milestones association.
project_1.milestones.length
# Still returns 0, because project_1 already cached it's copy of
# the milestones association back when there were 0.
project_2.milestones.length
# Now returns 2, because only project_2 knows about the new milestone.
project_1.milestones(true).length
# Returns 2, because ActiveRecord updates the cache
# when association(true) is present.
Another funny something to note about the association caching behavior is that even when ActiveRecord uses a cached value, it still emits SQL to the log file. So, don’t let that trip you up either.
Posted: March 31st, 2009 | Author: Brad | Filed under: Rails, Testing, ruby | Tags: cucumber, Rails, rspec, ruby, webrat | 1 Comment »
If you’ve read the new PragProg beta e-book on RSpec, you may have read that you can set HTTP headers for your Webrat request like so:
Given /^I am browsing the site using Safari$/ do
header "User-Agent" , "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us)"
end
Like me, you may have found out the hard way that this doesn’t work. Webrat does not automagically apply these new HTTP headers to your request – they certainly don’t make it to my controller. What worked for me:
Given /^I am browsing the site using Safari$/ do
headers["User-Agent"] = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us)"
end
When /^I visit my precious site$/ do
get '/my/precious/path', my_query_string, headers
end
In the code above, headers is a method call that returns all the HTTP headers for your request. Just tack headers on as the third argument of your request, and you’re good to go.
Posted: March 23rd, 2009 | Author: abel | Filed under: Programming, Quick Hits, Rails | Tags: fastercsv, noob | No Comments »
Here are two things I discovered this weekend:
Thing 1: FasterCSV is really cool! It’s easy to use and does exactly what it should. Here’s Scott Becker’s exporting tutorial. For importing, i’ve hacked together bits from Peter Larkmund’s travels and the FasterCSV documentation. Thanks to these tutorials, AMB will give users the ability to upload and export CSVs.
Thing 2: Default values for the win! If you have a table that has a column for an amount, feel free to set the default value to 0.00. Otherwise you won’t be able to do the following:
a = Account.new
a.amount += 20
…because a.amount will be nil. You’ll throw an error and if you just updated a bunch of code, you might waste an hour trying to figure out why you’re throwing an exception. However, if you declare a default value, you’ll be able to avoid any issues.
If you want me to go into further detail on either thing, just leave a comment.
Thanks for reading and happy hacking.
Posted: March 15th, 2009 | Author: abel | Filed under: Programming, Rails | Tags: ambudget | No Comments »
I’m not new to software development, but unlike Gabe and Brad I’m new to hacking rails. I’ve created simple tutorial apps in rails (an online store, a blog, etc.) and I now feel comfortable enough to create something for the world to see. At the same time, I realized that I would need to care about the project in order to keep coding it. I decided to turn the spreadsheet that I use for budgeting into a rails app. AM-Budget (which I like to call AMB) is my first rails app and I’ve learned a lot leading up to its deployment. I’d like to share the following with you in the hopes that I can encourage other ruby-newbies to release a v1.
Read the rest of this entry »
Posted: March 1st, 2009 | Author: gabe | Filed under: Rails, Testing | Tags: assert_select, invalid selector | Comments Off
Problem: assert_select 'input[name=model_name[field_name]]' gives ArgumentError: Invalid selector: ]
Solution: assert_select 'input[name=?]', 'model_name[field_name]'
Thanks to the Boston Ruby Group thread for the answer.
Bonus: Here’s a helpful assert_select cheat sheet.
Posted: March 1st, 2009 | Author: gabe | Filed under: HTML, Rails | Tags: forms, invalidauthenticitytoken, tables | 3 Comments »
Recently, while pairing with Abel as we hacked on some code for his budget tracking app, we came across an interesting problem while trying to acomplish what seems like a pretty straightforward task.
Read the rest of this entry »
Posted: December 22nd, 2008 | Author: gabe | Filed under: Rails | Tags: named scope, Rails, search | No Comments »
When it comes writing elegant search code in a Rails app, Named Scopes immediately come to mind. And for good reason: they’re a fantastic way to express, well, scopes, for your searches. In your Person model, you might have named scopes like by_last_name, by_age, and by_sex. But, what do you do when you want to give your users a search form and let them find people by any combination of last name, age, and sex?
Read the rest of this entry »
Posted: November 22nd, 2008 | Author: Brad | Filed under: Great Minds, Javascript, Programming, Rails | Tags: gm, Great Minds, Javascript, message queue, messaging, Programming, Rails, rails rumble, ruby, ruby on rails, rumble | 1 Comment »
Our Rails Rumble 2008 entry, Great Minds (you can have a look at the latest version or the original Rumble version), required a messaging system. Such systems are easy to do wrong, and we knew we’d need something that would stay solid under unknown load during Rumble judging.
The solution was quick & dirty (as most solutions are during Rails Rumble), but the results worked, and allowed us to qualify for judging and reach a respectable 28th place finish in the “Completeness” category.
Check out the details after the fold.
Read the rest of this entry »