Quantcast

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
end

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

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 }
else
obj.symlink_kind? ? eval(obj.symlink_kind+'.find('+obj.symlink_id+')') : obj
end
end
end

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

You should follow me on twitter here.

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

4 Comments:

Anonymous Small Business Hosting said...

I wish this worked with people!

6:19 AM, October 01, 2006

 
Blogger Zhenhong Bao said...

michael kors outlet clearance
tiffany jewelry
tiffany jewelry
swarovski crystal
ralph lauren polo
ralph lauren,polo ralph lauren,ralph lauren outlet,ralph lauren italia,ralph lauren sito ufficiale
tiffany and co jewelry
ferragamo shoes
hollister uk
ferragamo shoes
kobe shoes
asics,asics israel,asics shoes,asics running shoes,asics israel,asics gel,asics running,asics gel nimbus,asics gel kayano
tiffany and co
toms outlet
cheap ray ban sunglasses
polo ralph lauren
cheap mlb jerseys
true religion sale
cheap jordan shoes
ralph lauren uk
polo ralph lauren
michael kors outlet online
mont blanc pens
rolex watches
ray-ban sunglasses
longchamp outlet
louis vuitton handbags
iphone case
burberry outlet online
juicy couture outlet
christian louboutin uk
omega watches
true religion jeans outlet
ferragamo shoes
bottega veneta outlet
20160531zhenhong

6:05 PM, May 30, 2016

 
Blogger Fangyaya said...

cleveland cavaliers jerseys
canada goose
rolex watches outlet
nike factory outlet
ralph lauren uk
basketball shoes
tory burch outlet
polo ralph kids
coach factory outlet
ralph lauren polo
michael kors outlet clearance
coach factory online
uggs on sale
christian louboutin pas cher
nike air max 90
ray ban sunglasses outlet
true religion sale
toms shoes
authentic louis vuitton handbags
coach factory outlet online
washington wizards jerseys
ugg boots
michael kors purses
jordan 11
ugg boots
uggs outlet
kate spade outlet
gucci handbags
cartier watches
chenyingying20160908

7:14 PM, September 07, 2016

 
Blogger chenlina said...

chenlina20161018
ugg boots
coach canada
moncler pas cher
tory burch outlet
uggs on sale
louis vuitton uk
official michael kors outlet
true religion outlet
louis vuitton outlet
jordans for sale

7:47 PM, October 17, 2016

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

 

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