On 30 Oct 2009, at 14:11, Andrew Threlfall wrote:

> The usual method of daisy-chaining them in the controller like:
> current_user.account.books.recipies.ingredients.etc...find(:id)
> will have a big overhead if we only want to check if the user can
> access a record such as ingredients.


I talked to Andy about this on Friday and have seen the actual  
specific problem code he's dealing with. In fact, it's v2.0 of code I  
wrote a couple of years ago. :-)

My suggestion (and I've just hinted at it on GeekUp in a query on  
optimisation), is to chain it the other way around. This scoping is  
being used to basically say "Can this user see this ingredient?".  
Another way is to say "Is this ingredient in a relationship viewable  
by this user?" so you could:

Ingredient.find(:id).recipe.book.account.users

That gives me all the users up the belongs_to chain who can see the  
ingredient, so I could have a method on Ingredient to return  
"allowabled" users and a before_filter or a helper that determines if  
the current_user is in that group.

Chaining back up the belongs_to route is much more efficient in terms  
of CPU and RAM than lots of inner joins. I accept it's not perhaps the  
purist way to do things but it works and it's clean enough, IMHO.

--
Paul Robinson

http://vagueware.com :: [email protected] :: +44 (0) 7740 465746

Vagueware Limited is registered in England/Wales, number 05700421
Registered Office: 3 Tivoli Place, Ilkley, W. Yorkshire, LS29 8SU
Correspondence: 13 Crossland Road, Manchester, M21 9DU


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"NWRUG" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nwrug-members?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to