On Mon, Nov 2, 2009 at 1:54 PM, Paul Robinson <[email protected]> wrote:

>
> 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
>
>
> >
>
http://en.wikipedia.org/wiki/Law_of_Demeter

I think you've still got a bad Demeter violation, but I can't think off hand
how you'd fix it without resorting to hiding stuff in views or creating
another joining table that just does the direct mapping between the two
entities - maybe this would be easier, creating some triggers that police it
(in Ruby or MySQL, whatever)? Plus you've always got the old find_by_sql
method that you can hide. This might be better and more obvious.

I think the usual fixit approach of creating accessor methods (which is what
through is for) would just be a complete PITA and just push the Demeter
problem downward with zero gain.

Thanks and regards,

Francis

Follow me on twitter https://twitter.com/fjfish
Blog at http://www.francisfish.com
(you can also buy my books from there!)
Skype: fjfish

--~--~---------~--~----~------------~-------~--~----~
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