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 lina123 Chen said...

chenlina20150626
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
abercrombie
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

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

 

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