As an exercise in learning about HCards, I'm putting together a networking application for SXSW Interactive. Things are going along well--I love the vpim vcard gem!--but I've got a question about how to provide access to different types of contacts in my apps and still remain DRY. I have a User model, in which individuals are connected to each other self-referentially through a Friendship model. The User.rb file looks like this class User < ActiveRecord::Base has_many :friendships has_many :friends, :through => :friendships, :source => :contact, :conditions => [ "friend = ?", true ] has_many :colleagues, :through => :friendships, :source => :contact, :conditions => [ "colleague = ?", true ] ... (Btw, as you can see, the friendships table has a column for each sort of relationship--if anyone has a suggestion as to how to clean that up, I'd appreciate it...but that's not my main question...read on :-) I've created a Contacts controller to provide access to a user's contacts. The index method returns all of the user's contacts, so that if I visit a URL like this: http://localhost:3000/users/jacob/ contacts all my contacts are returned. I'd like to be able to drill down to any give type of contact, so I've created methods (accessible via GET, acting on the collection of a user's contacts) to access each type of contact, like so: def index @contacts = @user.contacts end def friends @contacts = @user.friends render_action :index end def colleagues @contacts = @user. colleagues render_action :index end This works as expected, but and it gives me nice URLs like http:// localhost:3000/users/jacob/contacts;met, but it's obviously not very dry, and besides, if I add a respond_to block (in either the index or friends, colleagues, etc. methods) none of the collection methods will return anything other than HTML--appending .xml to the link like http://localhost:3000/users/jacob/contacts;met.xml returns an error. Does anyone have any advice about how to go about DRYing up this sort of filtering? thanks in advance for your help, best, Jacob P.
on 2007-02-13 08:45
on 2007-02-13 09:52
On Feb 13, 8:43 am, Jacob P. <email@example.com> wrote: > if I add a respond_to block (in either the index or > friends, colleagues, etc. methods) none of the collection methods > will return anything other than HTML--appending .xml to the link like http://localhost:3000/users/jacob/contacts;met.xmlreturns an error. Wouldn't that look more like this? /users/jacob/contacts.xml;met As far as DRYing up is concerned, why not simply pass a type parameter as part of your requests, e.g. /users/jacob/contacts.xml?type=friends Ciao, Sheldon.