Ruby on Rails, Io, Lisp, JavaScript, Dynamic Languages, Prototype-based programming and more...

Technoblog reader special: $10 off web hosting by FatCow!

Thursday, June 08, 2006

Symlinking ActiveRecord Objects

Have you ever needed one object to act like another one? To symlink an object? In Rails it is quite easy to accomplish. Simply add a symlink_id integer field to your database table and use the following code.
class SomeObject < ActiveRecord::Base
def id
return symlink_id? ? super : symlink_id

def symlink(obj)
update_attribute :symlink_id, obj.id

There are many uses for this idea, but unfortunately I can’t reveal my use at the moment. Feel free to post comments with ideas though.

A notable limit to this code is that you can’t symlink objects that aren’t also SomeObject. It is possible to accomplish this as well however. In addition to symlink_id, add a varchar symlink_kind.
class SomeObject < ActiveRecord::Base
class << self
def find(*args)
obj = super(*args)
if obj.is_a? Array
obj.map{|o| o.symlink_kind? && o.symlink_id? ? eval(o.symlink_kind+'.find('+o.symlink_id+')') : o }
obj.symlink_kind? ? eval(obj.symlink_kind+'.find('+obj.symlink_id+')') : obj

def symlink(obj)
self.symlink_id = obj.id
self.symlink_kind = obj.class.to_s

You should follow me on twitter here.

Technoblog reader special: click here to get $10 off web hosting by FatCow!


Anonymous Small Business Hosting said...

I wish this worked with people!

6:19 AM, October 01, 2006

Blogger lina123 Chen said...

toms shoes
air max shoes
jordan 11 concord
louis vuitton outlet
kids lebron james shoes
michael kors outlet
ralph lauren sale
burberry bags
ray ban sunglasses
ralph lauren outlet
hollister clothing store
michael kors bag
red timberland boots
michael kors uk
celine handbags
michael kors handbags
coach outlet
concords 11
air max 95
coach factory outlet
coach outlet online
coach outlet store online
true religion jeans
polo outlet
louis vuitton uk outlet
tod's shoes
louboutin shoes
pandora bracelets
abercrombie and fitch
louis vuitton
coach outlet
michael kors
michael kors
cheap toms sale for kis
jordan 13 shoes
louis vuitton
ray ban sunglasses outlet
ray ban sunglasses
chanel outlet online

6:22 PM, June 25, 2015

Blogger Nesma Ali said...

شركة تنظيف مجالس بالرياض
شركة تنظيف شقق بالرياض
شركة تنظيف منازل بالرياض

شركة تنظيف بيارات بالرياض
شركة رش مبيدات بالرياض
شركة مكافحة حشرات بالرياض
شركة عزل خزانات بالرياض
شركة كشف تسربات المياه بالمدينة المنورة
شركة تسليك مجاري بالرياض
شركة تنظيف خزانات بالرياض

1:26 AM, September 02, 2015

Blogger Nesma Ali said...

شركة تخزين اثاث بالرياض
شركة الصفرات لكشف التسربات
شركة نقل اثاث بالرياض
شركة تنظيف فلل بالرياض
شركة تخزين عفش بالرياض
شركة نقل عفش بالرياض
شركة تنظيف موكيت بالرياض

1:26 AM, September 02, 2015

Blogger chenlina said...

ralph lauren outlet
barbour uk
nike sb
cheap jerseys
snapbacks wholesale
ed hardy uk
longchamp pas cher
sac longchamp
instyler max
jordan shoes
christian louboutin
swarov ski jewelry
cheap ray ban sunglasses
ralph lauren
coach outlet canada
nike running shoes
gucci shoes
ghd hair straighteners
ray ban outlet
longchamp handbags
christian louboutin uk
cheap uggs
michael kors outlet online
sac longchamp pliage
michael kors outlet
cheap versace
north face
barbour jackets
true religion jeans
kevin durant shoes
longchamp outlet
true religion outlet
prada uk
nike huarache trainers
cheap uggs boots
vans shoes
louis vuitton outlet
hermes uk
chaussure louboutin
ray bans

1:15 AM, September 14, 2015

Blogger Da Shuai Guo said...

prada shoes, http://www.pradashoes.us/
the north face outlet store, http://www.thenorthfaceoutletstores.org/
the north face clearance, http://www.thenorthfaceclearances.us.com/
michael kors outlet online, http://www.michaelkorsoutletonlinestores.us.com/
air jordan 11 free shipping, http://www.airjordan11.net/
discount oakley sunglasses, http://www.oakleysunglassesdiscount.us.com/
chicago bears jerseys, http://www.chicagobearsjerseys.us/
nike free run uk, http://www.nikefreerunning.org.uk/
golden state warriors jerseys, http://www.warriorsjersey.com/
oklahoma city thunder, http://www.thunderjerseystore.com/
oakley sunglasses, http://www.oakleysunglasses-outlet.us.com/
north face outlet store, http://www.thenorthfaceoutletstores.org/
nike huarache, http://www.nikeairhuarache.org.uk/
valentino outlet, http://www.valentinooutlet.us.com/
louis vuitton bags, http://www.louisvuittonhandbagsoutlet.co.uk/
packers jerseys, http://www.greenbaypackersjersey.us/
ugg boots clearance,ugg australia,uggs on sale,ugg slippers,uggs boots,uggs outlet,ugg boots,ugg,uggs
coach outlet online, http://www.coachoutletcanada.com.co/
nike air max 2014, http://www.airmax2014.net/
los angeles clippers jerseys, http://www.clippersjerseystore.com/
louis vuitton outlet, http://www.louisvuittonoutlet.in.net/
mcm outlet, http://www.mcm.in.net/
louis vuitton handbags, http://www.louisvuittonhandbags.org.uk/
longchamp outlet, http://www.longchampoutlet.name/
michael kors uk, http://www.michaelkorsbags.uk/

1:06 AM, September 15, 2015

Blogger Minko Chen said...

michael kors outlet
barbour jackets
tiffany and co
ed hardy clothing
miami dolphins
toms outlet store
coach handbags outlet
true religion outlet
nfl jerseys
snapbacks wholesale
true religion outlet
pittsburgh steelers
oakley sunglasses wholesale
cyber monday deals
mac cosmetics
canada goose outelt
canada goose jackets
abercrombie and fitch
michael kors outlet
michael kors factory online

9:07 PM, November 13, 2015


Post a Comment

Subscribe to Post Comments [Atom]

<< Home


If you like this blog, you might also like top photography schools.