Rails Nested Resource Issues

Martyn Taylor mtaylor at redhat.com
Thu Sep 20 14:59:42 UTC 2012


Thanks for this, I'll add it to the README and forward this mail along 
to the rails list.

Cheers

Martyn

On 09/20/2012 03:34 PM, Jiří Stránský wrote:
> I'd prefer the controller approach, since that's where the problem is 
> IMO and where it should be solved. And it seems to me the controller 
> way has the lowest amount of evil present.
>
> I'd stay out of touching the addresses= method. I have a panic fear of 
> changing any magic of ActiveRecord to different magic. I'm a bit 
> afraid this would come bite us in the buttocks later.
>
> Custom XML parser is probably safer, but it still somehow doesn't seem 
> right to me that:
> '<addresses>...' --> parse_xml_into_hash --> { 'addresses_attributes' 
> => ...}
> I think this is not the layer where the problem should be solved.
>
> IMO the very essence of the problem is that we get params which 
> ActiveRecord doesn't understand out of the box. Controllers are the 
> right place to transform params so that our models understand them. 
> (At least this is how the whole situation appears to me.)
>
> With a little luck, this could be solved with a single before_filter 
> that would alter params directly (evil, but controller actions stay 
> untouched) or create something like transformed_params to be used 
> instead of original params (not evil, but we have to use a different 
> variable in controllers). I believe this can be solved in a reasonably 
> generic way, so for each controller you'll have just something like:
>
> before_filter { transform_associations_from_xml('user' => 
> ['addresses']) }
>
> and to illustrate a more complicated case:
>
> before_filter { transform_associations_from_xml('user_group' => 
> {'users' => ['addresses', 'phones']}) }
>
> Or maybe the keys that have to be changed could be autodetected from 
> how the Hash is structured, so it might even become:
>
> before_filter  :transform_associations_from_xml
>
> Not sure if the last one ^^ is a good idea, we'd have to give it more 
> thought.
>
> So how about that?
>
>
> Btw Martyn you described the problem very clearly and the sample app 
> will enable us to hack on some solutions. Way to go for similar stuff ;)
>
> J.
>
> On 20.9.2012 14:20, Martyn Taylor wrote:
>> Gents.
>>
>> I'm having some trouble with getting nested resources working 
>> properly from bespoke XML/JSON in RESTful API.  There seems to be 
>> some fundamental problems with the way rails works, which means we 
>> have to write a lot of bespoke code to get it working, possibly even 
>> having to monkey patch Rails itself.
>>
>> I've written an example rails app that highlights the problem and put 
>> a detailed description in the README you can find it here:
>>
>> https://github.com/mtaylor/Rails-Nested-Resource-Issues
>>
>> I've ran into this issue on IME.  But it's going to affect us across 
>> all rails projects with REST APIs, so we could do with discussing 
>> this issue and coming up with the best possible solution that results 
>> in as little replication as possible.
>>
>> Please read through the problem and let me know if you have any 
>> better ideas that the solutions I have proposed.
>>
>> Regards
>>
>> Martyn
>





More information about the aeolus-devel mailing list