Hi Guys!
Thanks for both of your input.
I've opted for a helper method that looks something like:
module UsersHelper
def has_relationship_with?( other_user )
var_name = "@has_relationship_with_#{other_user.id}"
instance_variable_get(var_name) || instance_variable_set(var_name,
current_user.has_relationship_with?( other_user )
end
end
Loading all of the relationships was a little unnecessary in this case
but making the same db query more than once per request was too so
I've stuck the value in an instance variable
you reckon that's ok?
On Jul 27, 7:38 pm, James Englert <[email protected]> wrote:
> You might want to memoize the results of that function to prevent
> unnecessary hits to the database during the same request.
>
> On Mon, Jul 27, 2009 at 2:21 PM, Rob Biedenharn
> <[email protected]>wrote:
>
>
>
>
>
>
>
> > On Jul 27, 2009, at 12:26 PM, Ashley wrote:
> > > Hello guys!
>
> > > Question:
>
> > > Views should be "dumb" and models should deal with db queries...
>
> > > but what if you have a situation like so
>
> > > class User < ActiveRecord::Base
>
> > > def has_relationship_with?( other_user )
> > > !relationships.find_by_other_user_id( other_user.id ).nil?
> > > end
> > > end
>
> > > and then in your view:
>
> > > <%= "Display this message" if @user.has_relationship_with?
> > > (@other_user) %>
>
> > > Is it cool to call model methods which, in turn, query the db within
> > > the view or is there a better way of implementing this?
>
> > > Thanks
>
> > > ASH
>
> > This should be fine, but you might want to think about whether you
> > need to "eager load" the relationships assosciation for performance.
>
> > The benefit is that your model contains the code and you can
> > potentially refactor without touching your view.
>
> > -Rob
>
> > Rob Biedenharn http://agileconsultingllc.com
> > [email protected]
>
> --
> =====================
> Jimhttp://www.thepeoplesfeed.com/contribute
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" 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/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---