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 aHave 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_idinteger 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!
15 Comments:
I wish this worked with people!
6:19 AM, October 01, 2006
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
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
chenshanshan20170315
1:20 AM, March 15,:21 AM, August 27, 2017
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
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,:01 AM, April 26, 2018
zzzzz2018.7.16
nike requin
ray ban sunglasses
nike shoes for women
coach outlet
dsquared
kate spade outlet
longchamp outlet
supreme clothing
oakley sunglasses
saics running shoes
7:10 PM, July 15, 2018
zzzzz2018.9.2
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:06 AM, September 13, 2018
6:31 PM, October 11, 2018