Posts from has_many :through...
Posted by Josh Susser 34 days ago
I'm not sure where I first heard that you could do a recursive lamdba in Ruby, but it's been simmering on the back burner of my brain for a while. I've just never had a reason to use one, until now...
I wanted to process the Rails request params, which is a hash of strings and hashes of strings and hashes of strings and hashes... you get the idea. The need was to strip all the accent marks from user input throughout the application. Here's what I came up with:
class ApplicationController < ActionController::Base
before_filter :strip_accents
protected
def strip_accents
thunk = lambda do |key,value|
case value
when String then value.remove_accents!
when Hash then value.each(&thunk)
end
end
params.each(&thunk)
end
end
That's all completely clear, right? The filter enumerates the top-level hash using the &/to_proc operator to coerce the lambda to a block for the #each method. #each passes the key and value to the lambda, which either removes the accents from a string, or recursively enumerates the contents of a nested hash.
I think it's totally cool that you can do this in Ruby. Everyone thinks that Ruby is just an object-oriented language, but I like to think of it as the love-child of Smalltalk and LISP (with Miss Perl as the nanny).
Posted by Josh Susser 39 days ago
A couple of months ago I ran into a weird issue in my current Rails project that made no sense at all. All we did was add a lock_version field to a model to enable optimistic locking and suddenly things started breaking in a big way. After a bit of digging we found it was because ActiveRecord wasn't properly quoting a table name when updating a record with optimistic locking. I submitted a patch for that issue (so it's fixed in Rails 2.1), but lately I've seen a few similar bugs having to do with table name quoting in various circumstances. The amusing thing to me is that all of these bugs have one thing in common: they were uncovered by creating a model named Reference.
At first I thought this was a pretty big coincidence, but after just a moment's thought it seemed pretty obvious. ActiveRecord pluralizes model names to form conventional table names, and references is a reserved keyword in MYSQL. I guess Reference is a word that makes a good model name, especially if you're building a big data graph and can't think of a more specific relationship name, and it's about the only noun that pluralizes into a reserved keyword that anyone would ever use. In our case, we could have done a rename refactoring to change the model class name to CharacterReference. Instead we used an override and changed the table name to t_references, since that seemed like the least effort for a temporary workaround until the fix got released with Rails 2.1.
All these various issues with table name quoting are indeed bugs in ActiveRecord and should be reported and fixed. (There's also a major reworking of the internals of ActiveRecord in progress that should deal with virtually all of these issues in one fell swoop.) But in the mean time, you might want to avoid using model names that generate SQL reserved words, or just override the table name to something else.
Posted by Josh Susser 48 days ago
I'm finally recovered from RailsConf. Wish I could say the same thing for my car, which seems to have gone into a major funk from being ignored so much. Anyway, it's been cool reading all the aftermath posts and seeing people's thoughts and analysis of the conf. MagLev seems to be getting a lot of the attention, and I'm eager to see how things develop with it.
If you missed me at the conf, or saw me but just can't get enough of me, here's some links to a couple interviews people did with me there.
Gregg Pollack did some very nice video spots with many of the conf speakers. His RailsConf in 36 Minutes video is great. If you don't want to watch the whole thing (but why wouldn't you?), he posted the interview with me separately too.
Fabio Akita did some great podcast interviews at RailsConf. You can grab mine directly too.
As I said in my chat with Fabio, the next thing I'm going to do is release the Teldra code on GitHub. Stay tuned, it shouldn't be too long now.
Happy Friday!
Posted by Josh Susser 53 days ago
Got my RailsConf talk done yesterday. Seems to have gone over well, though my slides didn't show color well in the brightly lit room so they turned the lights down all the way to see them better and I ended up doing the talk as a ghost story for half of it. Guess I have to revise my rules for slide creation to account for ambient light.
Anyway, I have a PDF of the slides posted: The Great Test Framework Dance-off. I've also opened up the example code and tests on github as the teldra_prime project. That project is based on the application that runs this blog, but you should probably avoid deploying it to run your own blog. I'll be releasing the Teldra blog software as its own project very soon, and that will be the project where I continue development and people can contribute changes.
Also, it turns out I have a craaaaazy fan. Here's a picture of us at the end of the talk, me posing with his ballpoint tattoo of me on his bicep. Yes, somebody drew picture of me on his body! That so beats people introducing themselves at the urinal!
UPDATE: Alright, it turns out there is a way to significantly simplify the be_sorted custom matcher I showed as an example for extending RSpec. David Chelimsky sent me this alternate implementation that uses the simple_matcher helper:
def be_sorted
return simple_matcher("a sorted list") do |actual|
actual.sort == actual
end
end
That doesn't have all the power of the example I gave, but it is pretty much equivalent in operation to the test/unit example I gave and said was so much easier to write than one in RSpec. Nice one, David.
Posted by Josh Susser 54 days ago
DrNic complained about the lack of blogging from RailsConf, so here's a quick little update with some highlights. It's still early here and I have to get to breakfast soon, so I have to be brief.
First impression: lots of people. Second impression: "they want us all to fit in there?"
I called it exactly right. Joel Spolsky tried to pull a Ze Frank and did some random crazy presentation in his keynote, but it fell pretty flat. It was really polished and fairly amusing, but I was often insulted by either his blatantly sexist attempts at humor or his estimation of the audience's intelligence. It was nearly entirely content-free, and while he tried to develop a theme about the importance of esthetics, he never went anywhere with it. If you missed it, count yourself lucky.
On the other hand, David Heinemeier Hanson did a keynote where he transformed himself from a technologist into a life coach. His talk on The Great Surplus was interesting, if only to get a glimpse of the world from his perspective. I think most of what he said was right on, especially the bit about how everyone should code less and sleep more.
One of his points was his expectation about how Rails might lose its productivity advantage. He said there were three options: 1) Mainstream tech copies Rails' good points, 2) A radically new tech outdoes Rails, and 3) Rails becomes mainstream so there is no longer an advantage. But I think there is a 4th option. It's what happened to Smalltalk. I'm talking about C++. We Smalltalkers used to think the advantages of our language were so significant that it would take over the world. We had a huge productivity advantage over C coders. Then C++ came along and gave C coders just enough to let them improve their productivity and their ability to write larger more complex systems. It still wasn't as good as Smalltalk, but it was better than C, and much more accessible to most programmers than Smalltalk. C++ eventually sucked up all the oxygen and Smalltalk is now only a language for hobbyists and the occasional programming god. I think this is the most likely threat to the Rails surplus, that C# or Scala or something can do a good enough job that people can double their productivity with far less of a change in mindset or tools, and eventually no one will care about the ten times (or whatever) productivity of Rails. "Good enough is good enough."
Last bit before I gotta run. I've heard rumors about MagLev for a while, and the early announcement last month got me really excited. The demo and discussion yesterday by Avi Bryant and Bob Walker was one of those jaw-droppers that had everyone in the room freaking out. What they showed was pretty spectacular, though I always like to keep in mind Lansford's Corollary to Clarke's Third Law: "Any sufficiently advanced technology is indistinguishable from a rigged demo." The staggering performance boost together with the scalability and seamless integration of persistence could be a serious game-changer. I think the GemStone OODB technology beats the pants off of the ORM approach for most web applications, and if they can pull this off it's going to have a huge impact on how I write my software.
I'm giving my talk today at 4:25pm. Yes, I still freak out about giving talks. As critical as I can be of others, I'm hardest on myself. I hope I don't give myself too lousy a review when I'm done, heh.
Posted by Josh Susser 58 days ago
Seems like only last week that I was in Portland for RailsConf, but it was a whole year ago. That means it's time to do it again. I'm looking forward to the conference and getting to soak in all the Railsness. The program looks excellent this year, though I doubt Joel Spolsky can top Ze Frank or _why.
I always like to meet my readers, so please do say hi. If you want to know where to find me, I've got a few fixed points on my schedule:
- 7:30pm Thursday - Pivotal Labs BoF. We were going to talk about our open source projects, but instead we're going to talk about the agile development process and Rails.
- 12:30pm Friday - Book Signing. I've got a recipe in Mike Clark's new Advanced Rails Recipes book, and a bunch of us contributors will be signing copies. Look for us in the Powell's Books booth. I'm pretty sure Mike is buying me some Scotch for this, so make it worth his while.
- 4:25pm Saturday - The Great Test Framework Dance-off. I'm doing a talk comparing the most popular test frameworks. I don't know if it will be Adam Keyes level great, but I promise there won't be karaoke.
- 9:00pm-Midnight Saturday - Pivotal Labs Beer Night. Free drinks, food and pool. At the Rock Bottom Brewery, 206 SW Morrison St.
Also we're raffling off a Nintendo Wii in our swiipstakes on Saturday. Come to the BoF on Thursday to get one of our shirts, or if you can't be there you can make nice with a pivot later to scam one if we have extras. If a pivot spots you in a shirt over the next three days you can get a raffle ticket. (Wear the shirt while presenting a talk to get two tickets!) Then come to the beer night for the Wii raffle (must be present to win).
Posted by Josh Susser 79 days ago
A project I'm working on now is up to 57 model classes and is still growing. That's a lot of classes - welcome to domain modeling. In my opinion, the number of classes is a fair tradeoff that keeps each class simple enough to understand. In some ways it moves complexity out of the model class internals into the inheritance hierarchy, which is an important part of object-oriented design. I've worked on projects with many more model classes than that too. (Financial applications seem to require a lot of classes to model the complicated workflow and permission systems.)
The place where it starts to get hard to manage is when I look at the file system and see so many files in one directory. My brain usually starts to overload when I see more than a dozen or so classes in a directory. My first inclination is to throw some related class files into a subdirectory. The problem is that the standard way to do that in Rails is to put those models classes in a namespace (module). Rails used to have big problems with namespaced models, mainly with the dependency auto-loading code that finds class files based on the model class name. Most of those problems have been fixed, but there are still some usability issues with namespaced models.
Posted by Josh Susser 96 days ago
Symbols are one of the basic features of Ruby that give it that certain charm we all love. They aren't unique to Ruby (look at Smalltalk or Lisp), but they are a fundamental piece of the language. I'm not going to review what symbols are in this article since there are plenty of other explanations a short google away. However, I do want to say a few words about what I consider a common misuse of symbols.
The way I see it, symbols are great for naming things in your code, but bad for using as domain data. Over the last year or so I've seen a growing number of cases where symbols are used as an alternate syntax for plain old strings. I guess some people like to see :thing instead of "thing" in their code. Well, I don't like it. Sure you get to save a character, but at what cost? Won't somebody think of the children?
Posted by Josh Susser 105 days ago
If you hadn't heard, GitHub had their public launch today. Congratulations to Chris, Tom and PJ on such an awesome product. I'm sure there's a bright future there.
I keep getting surprised by how different using GitHub is for me. Last week someone I never met or had even heard of found my migration_condordance repo and submitted a fixes for two bugs. It wasn't quite as big a thrill as my first kiss, but I sure got more of a rush from that than from seeing Beowulf in 3-D IMAX. On the other hand, when I saw someone else stopped watching my repo I was actually a bit sad. Yes, this is geek social networking with both value and impact.
You've probably heard that the Ruby on Rails is moving the official repo to GitHub. It's not active as of this writing, but give it a few hours. It's now active at http://github.com/rails/rails. I'm looking forward to seeing what this does to the contribution process. I expect there could be a rich ecosystem of forks of Rails where you can see a bunch of variations integrated into a consistent whole. A lot of folks keep what is effectively a fork of Rails, but it's often in the form of a collection of monkey patches. Using git means that those patches can be managed more effectively, and even made available to the public in a form that can be easily consumed. Then it becomes much easier to evaluate whether a change has enough support to justify including it in Rails core - just see how many people are actually using the change, instead of merely of the opinion that it might be useful. I don't know how to track how many people are using a repo that way, but I'm sure someone will think of something - maybe just a count of how many clones were made or tarballs were downloaded.
At any rate, today feels like some kind of milestone. Or perhaps a furlongstone.
Posted by Josh Susser 113 days ago
Simple things should be simple, complex things should be possible. — Alan Kay
Here's a tiny little tip for handling those boiler-plate pages that aren't part of your app's functionality but you usually need anyway. It's good for setting up about, contact, copyright, etc. You can always throw those pages into /public as static html files, but if you want them to get styled with layouts, they need to be rendered as actions. This is a way to do that simply. It's not rocket science, but I haven't done a noob post in ages and I'm getting over a cold and I haven't posted in too long so gimme a break.
Say you want to have a simple landing page and a few typical boiler-plate pages. Let's start with the routes.
In config/routes.rb
map.root :controller => 'home'
map.home ':page', :controller => 'home', :action => 'show', :page => /about|contact/
In app/controllers/home_controller.rb
def index
# render the landing page
end
def show
render :action => params[:page]
end
Throw your about.html or about.html.erb and other pages into app/views/home and you're good to go. If you've set up page caching, this won't even slow your app down.
The :page => /.../ bit in the route constrains it to match only those specific urls. If you want, you can change that to a constant, like HomeController::PAGES, so it's easier to manage.
If you want to link to those pages, you can use the route helper methods, home_path and home_url
link_to 'About', home_path('about')
You could always unroll the routes and have a separate route for each page, but I find this way a bit drier. But if you'd rather have a specific named route helper for each page, that's an okay way to go. Either way, you get to use layouts in your pages, and have a nice simple way to get them rendered.
Posted by Josh Susser 144 days ago
If you are a solo developer, Rails' migrations are the neatest thing since sliced bread. If you work on a team, you know that often it can be a real pain dealing with migrations. Someone on your team checks in a new migration and you don't notice it when you svn up or git pull, and suddenly all your tests are breaking. Or even worse, someone modifies an old migration and you need to reset and migrate up from zero (we're talking development here, not production). I'm not a fan of automatically running migrations (I'll leave the reasons for you to guess), but I do like to be informed of when I'm about to run headfirst into a wall. Saves so much wear and tear on my noggin.
And so I give you the migration_concordance plugin. It's pretty darn simple. From the README:
This plugin extends Rails migrations to provide notification when you need to run migrations. It
will detect both new migrations and modifications to previously run migrations. It is primarily of
use for team development, but is also useful when deploying a release to a new environment to
determine when migrations need to be run. This plugin does not run migrations automatically, but
will notify you whenever you need to run them.
Posted by Josh Susser 148 days ago
In Ruby, #length and #size are synonyms and both do the same thing: they tell you how many elements are in an array or hash. Technically #length is the method and #size is an alias to it.
In ActiveRecord, there are several ways to find out how many records are in an association, and there are some subtle differences in how they work.
- post.comments.count - Determine the number of elements with an SQL COUNT query. You can also specify conditions to count only a subset of the associated elements (e.g.
:conditions => {:author_name => "josh"}). If you set up a counter cache on the association, #count will return that cached value instead of executing a new query.
- post.comments.length - This always loads the contents of the association into memory, then returns the number of elements loaded. Note that this won't force an update if the association had been previously loaded and then new comments were created through another way (e.g.
Comment.create(...) instead of post.comments.create(...)).
- post.comments.size - This works as a combination of the two previous options. If the collection has already been loaded, it will return its length just like calling #length. If it hasn't been loaded yet, it's like calling #count.
That's I always have to look up these differences, so now I have them in one place so I don't have to think about it anymore.
By the way, today is my blog's second birthday. I just couldn't let that go by without a post!
Posted by Josh Susser 175 days ago
Page-caching is one of the highest leverage features in Rails. It doesn't take much to set up, and the payoff is huge. When building Teldra I knew from the start that page caching would be part of my production deployment, as it should be for any site with pages where content changes infrequently relative to number of views.
The only thing I find annoying about using the page caching feature is how the cached pages are stored in the RAILS_ROOT/public directory, right alongside all the app's other static pages. I greatly prefer having the cached pages stored in a separate directory. This makes it a lot easier to distinguish between static pages and cached dynamic pages, and if something goes wonky with your cache you can blow it away easily with a single command.
Posted by Josh Susser 180 days ago
The has_many :through feed has moved. Please update your feed reader to use the feed URL http://feeds.feedburner.com/hasmanythrough
Posted by Josh Susser 180 days ago
When I changed my blog software from Typo to Mephisto, my article and feed URLs all changed. I didn't want to break all those old URLs, so I put a lot of effort into writing a ton of Apache mod_rewrite rules to redirect the old URLs to the new ones. I set them up as 301 permanent redirects to indicate that the old URL was defunct and to use the new one from then on. That means that the feed reader is supposed to change the URL for the feed permanently. But feed readers are lame and many treat the redirect as a temporary change, so I'm still getting requests for feed URLs that haven't existed in over a year.
If you're reading this article in your browser because your feed broke and you came here to see what happened, that means it's time to update the URL for your feeds. I'm sorry, but your feed reader has been told every hour for the last year that the URL has moved to a new location forever, but it chose to ignore that fact, and I'm not going to keep supporting those old URLs anymore. Here's the new URLs so you can manually update your feeds:
The main feed is hosted on FeedBurner: http://feeds.feedburner.com/hasmanythrough (and has been for the last year).
Categories have disappeared and articles are now organized only by tags. Old categories are now just tags. Tag feed URLs look like: http://blog.hasmanythrough.com/tag/rails.atom
I haven't set up comment feeds yet, but they should be along in a week or two at most.
Posted by Josh Susser 181 days ago
I received word this week that my talk proposal for RailsConf 2008 has been accepted. I'll be giving a talk called The Great Test Framework Dance-off. Doesn't that sound like fun?
Posted by Josh Susser 182 days ago
I started this blog nearly two years ago. In its first incarnation, it was running on the venerable Typo engine, hosted on DreamHost. About a year ago I switched over from Typo to Mephisto, still on DreamHost. Typo was a great start for me, and Mephisto was a good change when Typo was having some issues with the project. (Typo seems to be back on track these days, and has been for a while.) DreamHost was really cheap to get started on with hosting a Rails app, but I outgrew it in a couple months and have just been living in pain and denial ever since.
Now it's nearly two years later, and this is my 100th post on this blog. Coincidentally, it's another big transition for me. As of now this blog is hosted by the awesome force of nature that is EngineYard, and it's running on blog software of my own creation.
Posted by josh 218 days ago
Here's another entry in my ongoing Rails book review series.
Title: The Rails Way
Author: Obie Fernandez
Publisher: Addison-Wesley
The latest entry in Addison-Wesley's Professional Ruby Series is The Rails Way, by Obie Fernandez. The name is a nod to Hal Fulton's noble classic The Ruby Way, and it's clear this book aims to be as significant a feature in the Rails publishing landscape. The good news is that the book delivers, and then some. The Rails Way appears destined to become the new bible of Rails development.
Before I dive into the full review, a disclaimer. Obie is a friend and fellow cabooser, and someone I have a lot of respect for. I just want to get that out of the way so that no one can say I'm shilling for his book on the sly. But I'm glad I can give him a good review with a clear conscience. Anyway, back to the review.
Posted by josh 254 days ago
Wow, RubyConf was awesome. I'm sure by now you've read all the various blog reports on the sessions and the werewolf attacks. Aside from some stupid, amazingly loud all-night construction across the street from my hotel room, the conference was great and I had a superb time. It was very cool to see how far alternate Ruby VMs have come, and I expect the next year to be very interesting in that area. By the way, RejectConf has become too mainstream. What's up with doing it in a real conference room with A/V support and everything? And what about the beer?
Thanks to everyone who introduced yourself to me as a reader. (Though in the future you might want to wait until I've washed my hands and left the bathroom.)
The QCon panel last week was fun too. The topic was When is Rails an Appropriate Choice? James Cox did a nice job of running things, and I got to spend an hour talking with Obie Fernandez, Charlie Nutter and Ola Bini. (There were some last-minute changes in the lineup.) The panel was recorded on video, so I'm guessing InfoQ will make that available at some point.
Oh yeah, some news. Last week I started work at Pivotal Labs here in SF. Pivotal is a real powerhouse consulting firm specializing in web app development and does work in both Java and Ruby on Rails. The Rails developers there are top-notch and I'm really happy to be part of such a talented team. If you haven't been reading the company's coding blog Pivotal Blabs, you've been missing out. While I was having fun consulting and getting to work on lots of different things, I was missing having a regular schedule and co-workers who lasted more than a short time. This way I get the best of both worlds. I also get to work in an environment with a solid commitment to good development practices. My new job title is "Senior Agile Engineer", which should tell you something even if it mystifies the bank next time I apply for a loan.
Posted by josh 268 days ago
This article updates a previous version for the Rails 2.0 way of things. Since there's not much difference, I decided to fix up the example code to be more understandable. After all, not everyone is a discrete math geek.
This example updates the one from the previous article. The only significant difference is that you don't need to specify the :foreign_key when using the :class_name option in a belongs_to association. In Rails 2.0, the key is inferred from the association name instead of the class name. I also included the :dependent option because I feel it's too often overlooked.
These classes could be used to model a food chain. Spider eats fly, bird eats spider, cat leaves bird on pillow as gift...
create_table :animals do |t|
t.string :species
end
create_table :hunts do |t|
t.integer :predator_id
t.integer :prey_id
t.integer :capture_percent
end
class Animal < ActiveRecord::Base
has_many :pursuits, :foreign_key => 'predator_id',
:class_name => 'Hunt',
:dependent => :destroy
has_many :preys, :through => :pursuits
has_many :escapes, :foreign_key => 'prey_id',
:class_name => 'Hunt',
:dependent => :destroy
has_many :predators, :through => :escapes
end
class Hunt < ActiveRecord::Base
belongs_to :predator, :class_name => "Animal"
belongs_to :prey, :class_name => "Animal"
end
The Hunt model describes how likely a species of predator is to catch a species of prey. From the predator's perspective the hunt is a pursuit, but the ever-hopeful prey sees it as an escape. Note that you can model both kinds of hunts between the same pairings of animals: Some days you get the bear, some days the bear gets you.