bookmark_borderWhat the Flock

I downloaded Flock today, just to test create blog posts directly from Flock into socialroots repository, as a part of the project.

But I ended up using it the whole day. Just didn’t feel like going back to FF. Both of them are built on top of the mozilla engine and have the same shortcuts, this really reduce the (psychological and habitual) barriers to the change.

I added a couple of feeds that I used to read via bloglines to Flock; and now i am writing this blog post also in Flock.

Have yet to try out flickr and delicious functions inbuilt in flock.

Even Flock UI seems better than FF ( or is it just that i am liking the change)

Give Flock a try if you still haven’t.

bookmark_borderemacs for windows: meadow

Meadow is a tool that I came across on my last visit to Japan.
It is a GNU Emacs imlpementation for Windows and is fairly popular in Japan.
Though i had tried the windows version of xemacs before, I like the meadow interface much more.

You can get it here.
There is an english version of the wiki here.

I sometime do a little bit of rails hacking on my windows pc too … so i installed the ruby mode.
If you would want to add ruby-mode, you can find ruby-mode.elc under your ruby installation.
Copy it to lisp/progmodes/ under meadows home.

To customize Meadow further… here is a good link ( tis in japanese, but you should understand the lisp)

bookmark_bordervalidating Rails association

Should ActiveRecord add validations based on associations defined in the models?


class User < ActiveRecord::Base 
  has_many :posts 
end 

class Post < ActiveRecord::Base 
  belongs_to :user 
end 

If i create an instance of a post with a user_id that does not exist in the Users table, I wish ActiveRecord threw up a validation error on saving this instance of post.
It does not do so by default, so here are the validations that I add to my models to implement this.


class User < ActiveRecord::Base 
  has_many :posts 
end 


class Post < ActiveRecord::Base 
  belongs_to :user 
  validates_associated :user 
  validates_presence_of :user 
end 


Now if I try and save a post without a user_id or with a user_id that is not found in the Users table, an exception is thrown.

Till now I always use to create foreign keys in my database, as a safety net. So that the db throws an error if my data is not consistent.
After adding these validations, I hope not to have foreign keys in the database anymore.

bookmark_borderPolymorphic Association in Rails

Here is an account of my first use of Rails 1.1’s polymorphism in Model associations.

I had looked at all the examples at the wiki and a couple of other blogs but still wasn’t clear.

You appreciate most, the problems that you solve yourselves 🙂

Here is the problem I solved using Polymorphic associations.

Consider the scenario where there are Users of an application; who can act as both buyers and providers. Each user can infact have multiple buyer and provider profiles.
The application also has a messaging system for users to contact each other.
As a part of the application requirement, we need to keep track of the profiles of the message sender and receiver.

A user logs in using the login name and password present in the User model. I am omitting details so please assume for this example that each logged in user, has either a buyer profile or a provider profile associated to it, when they use the messaging system.

Here are the model files

A user can have many buyer_profiles and many provider_profiles


class User < ActiveRecord::Base
  has_many :buyer_profiles
  has_many :provider_profiles
end

A buyer profile belongs to a user and can have many messages associated to it as a sender and as a receiver.


class BuyerProfile < ActiveRecord::Base
  belongs_to :user
  has_many :messages, :as => :sender
  has_many :messages, :as => :receiver
end

A provider profile also belongs to a user, and can have many messages associated to it as a sender and as a receiver.


class ProviderProfile < ActiveRecord::Base
  belongs_to :user
  has_many :messages, :as => :sender
  has_many :messages, :as => :receiver 
end

The message model holds the polymorphic magic, via the two interfaces sender and receiver. A message belongs to sender which can be polymorphic,either a buyer or a provider. And also a message belongs to a receiver, which again is polymorphic, either a provider or a buyer.


class Message < ActiveRecord::Base
  belongs_to :sender, :polymorphic => true
  belongs_to :receiver, :polymorphic => true
end

The code above requires that I have these four special fields in my messages table to handle polymorphism. Here is the migration for messages


class CreateMessagesTable < ActiveRecord::Migration
  def self.up
    create_table :messages do |t|
      t.column :sender_id, :integer
      t.column :sender_type, :string
      t.column :receiver_id, :integer
      t.column :receiver_type, :string
      t.column :subject, :string
      t.column :message, :string
    end
  end

  def self.down
    drop_table :messages
  end
end

I ran the following code on the console to check if everything is working as expected.


r = BuyerProfile.find(1)
s = ProviderProfile.find(1)
m = Message.new
m.sender = s
m.receiver = r
m.subject = "test subject"
m.message = "test message"
m.save!

And it worked 🙂

I could access profles and users from the message object like this.


m = Message.find(1)
m.receiver                 # refer to the receivers profile object
m.receiver.class          # get receiver type, where BuyerProfile or SellerProfile
m.sender                   # refers to the sender profile object
m.sender.class            # get sender type, where BuyerProfile or SellerProfile
m.receiver.user           # refer the User object of the message receiver
m.sender.user             # refer the User object of the message sender

Polymorphism saved me a lot of time and also a whole new table which I was thinking of adding to handle this functionality prior to polymorphism.

Added : And yes, now I am thinking about what I would need to write to get all the messages sent or received by a User.

bookmark_borderDelhi ruby enthusiasts meetup.

Love Ruby. Love Rails. Love Beer. Love Food. Do You?

Jon and I are organizing a ruby/rails enthusiasts meetup on Friday, June 2, 8pm at Piccaddily, PVR Plaza, C.P.

We’ll be exchanging views and experiences on Ruby and Rails over beer and food.

All Ruby enthusiasts who are willing to share the bill are welcome to join us.

A rsvp mail to me or jon would be appreciated.
Coz Picaddily gets crowded at weekends at times … and we might need to make a reservation.

bookmark_borderRailsConf Europe 2006…. here i come.

Signed up for RailsConf Europe 2006, London September 14-15. It’s EXPENSIVE as compared to Chicago RailsConf in June. But I think the airfare will even out things for me. And the chicago conference got booked so fast, that I didn’t even get a chance to think seriously about attending it.

The conference is already packed with exciting speakers, including the creator of Rails, David Heinemeier Hansson, Pragmatic Programmer Dave Thomas, best-selling author and passion maven Kathy Sierra, Rails core developers Jamis Buck, Marcel Molina, Jr., Thomas Fuchs, Rails authors and trainers David Alan Black, and Chad Fowler, Rake author Jim Weirich and many more to be announced.

Sound like it’ll be fun …if you would be there too …. ping me and let’s meetup.