Saturday, October 25, 2008

Rails 2.0 - named_scope rocks..

In Rails 2.0, ActiveRecord has a feature called named scope, which makes things easier..

With named scope, you can give a name to scope or condition..For instance, if you have a model called "Comment" and say it has a database column called published, published_at, then the model with namedscope definitions:

    1 class Comment < ActiveRecord::Base
2 named_scope :published, :conditions => {:published => true}
3 named_scope :limit, lambda {|count| :limit => count}
4 named_scope :in_last_ten_days, lambda{:conditions => ["published_at > ?", 10.days.ago ]}
5 named_scope :order_by_published_time, :order => "published_at"
6 end
So,
Comment.published - return all the comments which are published.
Comment.in_last_ten_days - returns all the comment objects which has pusblished_at time within last 10 days.
Comment.limit(10) - returns 10 comment objects.
Comment.order_by_published_time - returns all the comments in chronological order.

They just behaves as find methods on the receiver with options just as we pass to the find method..

But the specality about named_scope is "Named Scope can be COMBINED and it will FIRE ONLY ONE SQL QUERY with all the options combined"

Like
Comment.order_by_published_time.limit(5) - returns top 5 comments in chronological order

For this operation one query is fired with ORDER BY and LIMIT clauses!!

With named_scope, we can also combine our own find method, like
Comment.order_by_published_time.find(:all, :condition => {:published_at => Time.parse "14 Jun 2008 15:38:06"})

Again, say we have another model called News and it has "has many" relationship with comment,

    1 class News < ActiveRecord::Base
2 has_many :comments
3 end
Then we can call the named_scope of comment for array of comments which are related to a news object, like

news = News.first
news.comments.published

For this operation also, only one query is fired for finding all the comments of that news which are published..

So named_scope can also be called for associated children objects of a parent model!!

Friday, August 15, 2008

Windows Vista cannot obtain an IP address from certain routers/DHCP servers

A month ago I bought a Dell Inspiron 1525 which has Windows Vista Home. I was surfing internet using ethernet and every thing was fine... Today i tried to connect to a wlan network... I was able to see the wireless network and i am also connected to the network.

Then comes the surprise!! I am not able to ping my peer machines in the network... Then i starting troubleshooting... The IP address assigned to my machine is unusal and that is not the one assigned by my DHCP server!!.. Then started googling and found out that by default in Vista, the BROADCAST FLAG IN DHCP DISCOVERY PACKETS IS ENABLED... So it is not able to obtain ip from certain type of routers or dhcp servers..

To disable it, (as usual there is no check box or some thing in network options.....but) you have to add an entry in registry...
  • Open regedit (run > regedit)
  • Open HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\
  • Select the GUID that corresponds to the device for WLAN (I found the guid in my machine with the wierd ip that vista assigned itself for wlan adapter)
  • add an entry with DWORD as DhcpConnEnableBcastFlagToggle and its value as 1.
  • or add an entry with REG_DWORD as DhcpConnForceBroadcastFlag and value 0.
  • Thats it...now restart your machine and you are connected to your network!

Tuesday, June 10, 2008

I joined Thoughtworks India on July 4th..

Im in Bangalore office right now for Thoughtworks University..
Orientation went fine during the first three days
And today is the second day of TWU..

Thoughtworks met my expectations to 100% and i'm satisfied much here...


I'm proud to be a Thoughtworker..
This video is too good...
Which it n enjoy..