SameShirtEveryDay.com

Personal blog of the one called Alex Gorbatchev, from Toronto, Canada.
follow me on Twitter

Getting acts_as_taggable gem to work

Posted on June 20th, 2007 by Alex Gorbatchev. In Rails. No comments yet...

require 'taggable'

Now you can use acts_as_taggable without any warnings. While this may seem obvious to many here, I hope it prevents the waste of a couple hours to a few other newbies thus my post.

Source. Definitely saved me time.

find_by_sql() custom columnts trick

Posted on June 18th, 2007 by Alex Gorbatchev. In Rails, Ruby. No comments yet...

Just stumbled on the fact that ActiveRecord automatically creates properties for all column in a SQL query, not just those in the table.

Model.find_by_sql("SELECT *, COUNT(*) AS rank FROM ...").each do |item|
  puts "%s (%d)" % [item.name, item.rank]
end

ActiveRecord associations without primary keys?

Posted on June 18th, 2007 by Alex Gorbatchev. In Rails. No comments yet...

I’ve looked low and high for this feature in ActiveRecord but can’t seem to find a solution.

What I need is ability to specify associations by a column other than a primary key. For example:

create_table :nodes do |t|
  t.column :parent_id,        :integer
  t.column :uid,              :string,    :limit => 32
end

create_table :info do |t|
  t.column :uid,         :string, :limit => 32
  t.column :formatted,   :text
end

class Node < ActiveRecord::Base
  acts_as_tree
  has_many :info, :foreign_key => :uid, :primary_key => :uid
end

class Info < ActiveRecord::Base
  belongs_to :node, :foreign_key => :uid, :primary_key => :uid
end

As you can see, I’ve used a made up :primary_key option to indicate which key column should be used. set_primary_key isn’t a good option here because UID column is for lose association and it isn’t a primary key.

Is this at all possible in Rails 1.2.3? Unfortunately my Rails core knowledge is very limited at this point and I wouldn’t be able to add this feature myself.

Leaving Mediatemple for Dreamhost

Posted on June 7th, 2007 by Alex Gorbatchev. In Noobkit, Rails. 1 comment so far...

So I’m really pissed with Mediatemple.

Not only it’s not the fastest dog on the block, especially the backend, they consistently kept shutting down Noobkit 5-8 times a day.

Mediatemple allocates 64MB of RAM for a Mongrel instance, which is fine. If your app goes over, they just shut Mongrel down, automatically… without restarting it. So I’m getting an SMS from Pingdom at 3AM telling me that the site is down and I have to go “cap restart” it.

Noobkit is a very small site currently and only been up and running for just over 10 days with barely 200 uniques a day. There’s absolutely no way Mongrel can tip over 64MB with this type of load. I tried running a production Mogrel locally and pound it with Apache’s AB. After 10,000 hits I’m seeing 45MB of RAM.

Response from Mediatemple to my question about frequent Mongrel shutdowns was as expected:

After reviewing our records, I was able to confirm that it has been shutting down repeatedly. However, testing it live versus on another system will not necessarily be an accurate estimate. You will either need to debug your application, or increase your container to another a larger server.

So yesterday I found a no-registration coupon and got myself an account at DreamHost. Moving the blog was a peace of cake. Setting up Capistrano was pretty easy as well and the DNS should be updated overnight.

At first, DreamHost seems much snappier. We’ll see how it works out.

open_id_authentication

Posted on June 6th, 2007 by Alex Gorbatchev. In Rails. No comments yet...

As always folks, all good ideas come to you right after your release the bad once.

After writing and making my user registration all pretty for Noobkit, I realized today, about 12 hours after release, that it would’ve been much easier and better if were to just use Open ID.

Having looked around for a decent acts_as_open_id_user_and_does_everything_else plugin, the only thing I found was this shameful attempt by none other than the DHH himself.

I don’t know if he was high when he submitted it because it’s just horrible. Never mind the fact that it doesn’t run at all and code has syntax errors. Methods that are called just once, strange object/result/message bubbling and integration with user models which are never mentioned anywhere except on the error pages. Worst of all, it actually extends ApplicationController. Do I really need Open ID authentication methods in every single controller? Where’s all the prettiness and tidiness?

Well, I brought my own bag of said “prettiness and tidiness”. The result is my own open_id_authentication plugin. I’ve cleaned up the code, made it a regular module to be included in a model and split up spaghetti dual purpose method into two simple methods. One to be called to start Open ID authentication process and the second one to process response from the Open ID server.

Next time, when I learn how to create generators in plugins I will add some nice model generation. For now, it’s completely decoupled from the user model.

Enjoy!

home
Subscribe to this blog Follow me on Twitter My bookmarks on Delicious My photography on Flickr