Jobs about this rails frameework.

作者:   發佈於:  

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: