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 Anonymous said...

I wish this worked with people!

6:19 AM, October 01, 2006

Anonymous Anonymous 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

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

7:14 PM, September 07, 2016

Blogger chenlina said...

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

Blogger chenmeinv0 said...

uggs outlet
louis vuitton
christian louboutin shoes
moncler soldes
cheap nhl jerseys
christian louboutin sale
moncler coats
steelers jerseys
uggs on sale
replica rolex watches

10:33 PM, January 03, 2017

Blogger Unknown said...

nike outlet
fit flops
ray ban sunglasses
cheap jordans
ralph lauren outlet
louboutin shoes
adidas super color
coach outlet
texas rangers jerseys
coach outlet store

1:20 AM, March 15, 2017

Blogger Unknown said...

louboutin outlet
yeezy boost 350 v2
adidas outlet online
true religion jeans outlet
nfl jerseys wholesale
ralph lauren uk
christian louboutin uk
cheap ray ban sunglasses
michael kors outlet online
birkenstock uk

7:01 AM, April 18, 2017

Blogger Đào Duy Quang Blog said...

Chung cư Green Pearl nằm trong quần thể Dự án Green Pearl tại số 378 Minh Khai, Vĩnh Tuy, Hai Bà Trưng, Hà Nội là một vị trí đắc địa thuộc cửa ngõ phía Nam của Thủ đô, tiếp giáp với đường Minh và KĐT times city. Căn hộ chung cư cao cấp Green Pearl sẽ mở ra không gian sống xanh, trong lành, KHÔNG ồn ào, KHÔNG khói bụi ngay trong lòng đô thị.
Chung cư Green Pearl

7:21 AM, August 27, 2017

Blogger ASO Services said...

Thanks a lot for sharing this with all of us, I like it and we can communicate. Do you need buy app ratings and reviews. To boost app ranking and double app downloads now.

7:55 PM, October 26, 2017

Blogger Cipro said...

A lot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Your topic give me more knowledge to share with my friends. Enjoy our kizi games, aquí está la página kizi for everyone! Thanks for that.

8:42 AM, March 29, 2018

Blogger Unknown said...

I don't apperceive what it is about the bendable blow and matte finish, but I accept a anemic atom for printed suede Christisn Louboutin Outlet. One of the best brands to cull this off is YSL Wallet Outlet. I accept to acquaint you about a backpack attraction of mine. I am in fact mad at myself that I did not splurge on the Ralph Lauren Polo Outlet Croc Muse Two which has two colors that I admire with a sueced croc pattern. To see a formed suede accomplishment off a Red Bottoms Outlet that I already adore, my affection melts. Replica Watches are additionally pretty reflective of their user's persona. Amazing furthermore multi-colored Replica Watches are commonly worn because of the younger. Conversely, exquisite dress Swiss Rolex Replica are regularly sported by highly developed gentlemen. And then usually there's the luxurious Swiss Replica Watches worn by the individuals who're properly to complete. Check out observing your mates furthermore you can before long see how the layouts are actually ranging.

2:01 AM, April 26, 2018

Blogger Unknown said...

nike requin
ray ban sunglasses
nike shoes for women
coach outlet
kate spade outlet
longchamp outlet
supreme clothing
oakley sunglasses
saics running shoes

7:10 PM, July 15, 2018

Blogger 5689 said...

converse shoes
canada goose outlet
moncler online
supreme shirt
nike shoes
michael kors outlet online
ugg boots clearance
ugg boots
christian louboutin shoes
nike soldes femme

6:23 PM, September 01, 2018

Blogger Đào Duy Quang Blog said...

Vincity Gia Lâm
Vincity Ocean Park
Vincity Ocean Park Gia Lâm
Tiện ích Vincty ocean Park
Mặt bằng vincity Ocean Park
Giá bán Vincity Ocean Park
Thiết kế vincity Ocean Park Gia Lâm
Vinhomes Ocean Park
Liền kề Oean Park
Biệt thự Vinhomes Ocean Park
Shophouse Vinhomes Ocean Park
Biệt thự Ocean Park
Vincity Đại Mỗ
Vincity Tây Mỗ

2:06 AM, September 13, 2018

Anonymous Tips Obat Tradisional Gagal Jantung said...

Thank you for joining us. his article is very helpful

Cara Mengobati Benjolan Di Vagina
Pengobatan Alami Penyakit vertigo
Obat Maag Alami
Obat Penambah Berat Badan
Cara Mengobati Penyakit Kista
Cara Mengobati Sesak Napas Alami

6:31 PM, October 11, 2018

Anonymous Cara Mengobati Penyakit Trigliserida Tinggi said...

the article is very petrifying, hopefully it can be useful and an important lesson

Pengobatan Penyakit Polip
Cara Mengobati Syaraf Kejepit
Cara Mengobati Paru-Paru Basah
Cara Mengobati Penyakit Herpes Dengan Bahan Alami
Cara Membasmi Penyakit Kudis Dengan Cepat
Cara Mengobati Kanker Usus Besar Yang Tak Kunjung Sembuh

1:11 AM, October 17, 2018

Blogger نور الهدى said...

ارخص نقل عفش بالرياض
دينا نقل عفش بالرياض دينا نقل عفش بالرياض
نقل عفش من المدينة المنورة الى مكة نقل عفش من المدينة المنورة الى مكة
نقل عفش من الدمام الى جدة نقل عفش من الدمام الى جدة
شحن عفش من جدة الى الاردن شحن عفش من جدة الى الاردن

2:11 PM, December 08, 2018

Blogger ahmed said...

في عديد من الاوقات قد يرغب الفرد في الانتقال من موضع إلى آخر و إن كانت من أكثر المشكلات التي تجابهه هي صعوبة نقل الأثاث حيث انه يتعرض إلى خسارة أو فقد الكثير من قطع العفش الضئيلة و الانتيكات أيضاً و نحو اللجوء إلى المؤسسات الغير معروفة من المحتمل ان تتعرض إلى الإستيلاء و التي تكلفك العديد من المال لذلكشركة نقل عفش
شركة نقل عفش من الرياض الى الدمام
شركة نقل اثاث من الرياض الى الدمام
شركة نقل عفش بحائل

3:15 AM, March 12, 2019

Blogger Golden Slot Game said...

golden slot

1:05 AM, June 29, 2019

Blogger samaher said...

ارخص شركة نقل عفش

ارخص شركة نقل عفش بالرياض

9:41 AM, July 25, 2019

Blogger Robert Smith said...

There could be many reasons for receiving the message "Brother Printer is Offline". Follow the easy steps in the article to resolve Brother Printer Offline Windows 10 issues. If you are still facing issues for the same you can reach our experts at our toll-free number.

1:02 AM, October 05, 2019


Post a Comment

Subscribe to Post Comments [Atom]

<< Home


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