has_one weirdness

I am not quite prepared to call this a rails bug. But has_one definitely behaves weirdly in rails 2.1.

Lets say you have the following two models:

class Email < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_one :email
end

A very straight forward has_one association. Now give the following from a script/console session:

>> user = User.first
=> #<User id: 1, created_at: "2008-07-09 01:19:11", updated_at: "2008-07-09 01:19:11">
>> user.email = Email.find(user.email.id)
=> #<Email id: 1, user_id: 1, created_at: "2008-07-09 01:19:26", updated_at: "2008-07-09 01:19:26">
>> user.reload
=> #<User id: 1, created_at: "2008-07-09 01:19:11", updated_at: "2008-07-09 01:19:11">
>> user.email
=> nil

That is weird. There is no reason you would do this in production, but if you were doing this you wouldn’t expect it to update the email row to set user_id to nil. Unfortunately, that is exactly what is happening.

Personally, I wouldn’t expect it to write anything to the database until I called save on user.

Anyone have a reasonable explanation, or should I submit it to Lighthouse.

Rails 2.1 Eager Loading

As long as I can remember you could always eager load your associations in rails. If you wanted to find all your users companies, you could tell rails to do it in an efficient way.

There is no change in the code you write

User.all(:include => :company)

The SQL it used to generate was:

SELECT
  `users`.`id`      AS t0_r0
  `users`.`email   AS t0_r1
  . . .
FROM
  `users`
  LEFT OUTER JOIN `companies` ON `companies`.id = `users`.company_id

Fine, that isn’t really news, but now active_record splits that sql statement up into two statements.

SELECT * FROM `users`;
SELECT * FROM `companies`
  WHERE `companies`.id IN ('1','2', . . . );

Apparently this will be faster in most cases. I don’t have anything to say about that but, there are two interesting things I want to show you. They both involve :conditions.

If you put conditions on the users table it still generates two queries.

User.all(:include => :company,
  :conditions => "`users`.`email` LIKE '%gmail%'")
SELECT * FROM `users`
  WHERE `users`.`email` LIKE '%gmail%';
SELECT * FROM `companies`
  WHERE `companies`.id IN ('1','2', . . . );

But if you put conditions on the companies table it fails over to the old style and generates one statement

User.all(:include => :company,
  :conditions => "`companies`.`name` LIKE '%google%'")
SELECT
  `users`.`id`      AS t0_r0
  `users`.`email   AS t0_r1
  . . .
FROM
  `users`
  LEFT OUTER JOIN `companies` ON `companies`.id = `users`.company_id
WHERE
  `companies`.`name` LIKE '%google%'

I don’t know if they have to do it that way to get the correct results or what. But I thought it was interesting.

The 3 Coolest things I saw at RailsConf

In no particular order

FiveRuns TuneUp

FiveRuns TuneUp

It is a tool for looking at what is make a request slow. The above screen shot shows the RideCharge landing page call stack.

Internally we hacked it so that it doesn’t run in development, but in a new environment development_tuneup. I also hacked it to ignore the idea of being logged in, to speed up adoption.

Passenger 2.0

I have long been a fan of mod_rails (now branded as passenger). There was a lot to be excited about in their talk. They talked about the release candidate features. But one of the most obvious (after you hear it) features was buffering file uploads. So, wait until the file is uploaded then dispatch to rails.

Pre Rails File Buffering

A Continued Commitment To Testing

The rails community has always been pretty committed to testing. It is our version of type safety. But, people are trying to do more and more testing in less and less time. There was a talk called “The Great Test Dance-Off” by Josh Susser. It was a pretty good summary of, Test::Unit vs. RSpec vs. Shoulda. David Chelimsky, RSpec Lead Developer, did a great write up on the talk. While I am a Shoulda man, I wholeheartedely agree with him that anything to get people testing and talking about testing is awesome.

RailsConf 08 ~ Day 2

RailsConf 08 day 2 was much better.

Jeremy Kemper gave the morning Keynote. It was about all the new shiny in rails 2.1, which is released today. He went over active_record scopes, migrations, time zones, and other things. I think the changes to migrations are brilliant and also painfully over due.

The first session I went to was on edge caching and ESI. It was given by Aaron Batalion. ESI is an alternative to fragment caching that is heavily used by akamai. I really liked this talk, it went at a good pace and the speaker was well informed.

The second session I went to was Advanced RESTful Rails by Ben Scofield. Unfortunately, my day job isn’t doing anything in a restful way. All that a side, I LOVED THIS TALK. I will definitely investigate how to start incorporating REST into my day to day.

The next session was Fast, Sexy, and Svelte: Our Kind of Rails Testing Dan Manges (ThoughtWorks), Zak Tamsen. They had some interesting ideas, and I really appreciate deep test. But, I don’t agree with unplugging your unit tests from the database which was one of their major points. I agree with their goals of making tests run faster and me more informative. I just don’t think the effort justifies the value. So, I will continue to have terrible average tests.

The next session was Integration Testing with RSpec’s Story Runner by David Chelimsky. I tried to go to a different talk, but I got there after the room filled up, so what I am about to say is as a person who didn’t want to go. RSpec Story Runner seems like an extreme pain in the ass. It is like why use one step when 100 will do. David was very well informed and well spoken, I like his style but not the content.

The last session, The Great Test Framework Dance-off by Josh Susser. I love his blog, and he is a great speaker with a very objective view point. And that is why he came to the conclusion that the best test framework is a matter of opinion. No surprises there!

And the Keynote by Kent Beck was a series of stories. About the “creation” of TDD, XP, and Design Patterns. It was very introspective and personal. He mentioned on several occasions the “watering down” of XP into “agile”. In his words, “who wouldn’t want to be agile”. I completely agree with that. The word agile is essentially meaningless. He came up with a new term, “responsible programming”. That basically refers to transparency and quality in the development process. I couldn’t agree more with the notion, but the phrase feels a little watered down (which I believe is the point.) So that makes an essentially good idea, 0 for 3 in naming in my opinion, extreme, agile and responsible.

I have so much respect for Kent Beck. He is a very humble and thoughtful person. I appreciated his talk more than anything at the conference. This is especially amazing considering it will have almost no practical impact on my day to day life.

RailsConf 08 ~ Day 1

Recapping the official start of rails conf.

The morning Keynote was by spolsky. He was entertaining, but mostly content free.

Dan Benjamin gave a talk called entrepreneurs on rails, I thought it would have something to do with rails, my mistake. It was still a good talk about making decisions to market and promote in addition to code.

Next, I went to a talk called the hosting woes, by the engine yard peeps. It was not that organized, more of a panel style discussion. They had some interesting things to share about how to structure your server environment. Like, put a few mongrels and a memcached instance on each “app” server, because that will be the most effective use of cpu and it scales more naturally.

Then I accidentally ended up in the DataMapper. I like DataMapper way more than I thought I would. I still don’t know if I have a place for it in my life right now, but I will definitely evaluating it when I start a new rails project.

The rubinious talk was awesome. I think in 3 - 6 months it will be production ready. One very cool thing they did was implement a version of eval in ruby in like 12 lines of code. It mad me feel like I could contribute, which I believe was the point.

DHH keynoted about, well, I am not sure. I guess it was about “the surplus”, which refers to the productivity “surplus” we have in the rails community. Mostly about how either rails will become “mainstream” or it will be passed by something else. Then the second part of his talk was about using that “surplus” to become a better programmer. That way if/when the good times end you will still have a competitive advantage. Some of his assertions were you should, sleep more, do things not in programming, read books not tech related, programming less, start something from scratch, start sharing, and spread the good word of rails. Mostly I think, if you read signal vs. noise, it was kinda obvious. Still, it was good to here it reiterated. Although, it kinda seemed like a sermon, mostly information you already know, but when you hear it, it makes you happy. But mad props for using the phrase, “go dubai”.

Again, more to come later, but if you want less information more frequently, you can follow me on twitter @schlu.

and/or subscribe to my RailsConf ‘08 flickr stream.

Feeds/Syndication