Blog to discuss Midnight Coders products features, ideas and trends in development of Rich Internet Applications

Thursday, September 07, 2006

Active Record Associations in Flex with WebORB on Rails

We've recently added a new feature to WebORB that allows native serialization of associations within active record models. This has been requested several times and now allows you to do things like (from the Rails documentation):

Person.find(1, :include => [ :account, :friends ])

...in your rails remote service and then in your Flex client you can very naturally peel off account or friends information directly from the person object that is returned.

var person:Object = event.result;
...person.name
...person.account.balance
...person.friends //(this will point to an array of friend objects)

This is something that has been needed since our first release and we are glad to get this out; now developers won't have to roll this data into a Hash in a unnatural way. Thanks to Brian Hogan for pointing me to the script/console feature of Rails to look under the hood and figure this out. I had heard about how cool this was at RailsConf, but never really appreciated it until solving this problem.

If you are interested in how it works, WebORB now inspects the instance variables of active record models; these objects always contain a set of "attributes", but also contain other properties if associative data was included in the find call. WebORB uses this knowledge to dig deep within a model object in order to reconstruct the entire result set and send it over the wire. Of course if you don't want this then simply don't "include" associations in your query.

Enjoy.

~harris

0 Comments:

Post a Comment

<< Home