Home

Advertisement

Previous Entry | Next Entry

Jobs about this framework

  • May. 19th, 2007 at 9:13 PM

Recenty used Rails for $work, and came up this function to make it more convenient to retrieve remote records in a many-to-many relationship with a join-table used to saved the relationship and it'll takes a .map-conversion away to get the actual records.

Here's what we came up:
module AsynapseSupport
def has_(prefix, item)
prefix = prefix.to_s
item = item.to_s

sy = "#{prefix}_#{item}"
mo = sy.camelcase.singularize

self.class_eval <
has_many :_#{sy}, :class_name => "#{mo}"
def #{sy}
_#{sy}.map { |f| f.#{item.singularize} }
end
EOE
end
end
If your mixin this module in your ActiveRecord::Base, then you can say this in your user.rb model class:
  has_ :favorite, :chatrooms
It'll make an favorite_chatrooms method to retrieve a list of "Chatroom" instead of retrieving a list of "FavoriteChatroom", which is the behaviour if you say:
  has_many :favorite_chatrooms
Our has_ method, although seems to be a controversial name, is pretty handy to deal with many-to-many relationship.

Project asynapse: http://code.google.com/p/asynapse/

Latest Month

September 2008
S M T W T F S
 123456
78910111213
14151617181920
21222324252627
282930    
Powered by LiveJournal.com
Designed by Lilia Ahner