Find and include problem

Hi,

I’m trying to reduce my queries to the database so i use includes, but
there is produced a query every time and i can’t see an improvement.
What does i’m doing wrong?

In controller:

@matches = @stage.matches.find(:all,:include =>
[:team_1,:team_2, :match_tips])

In view:

<% @matches.each do |match| %>
<% if @tipped_matches.include? match %>
<% tip = match.match_tips.find_by_user_id(current_user.id) %>

So the third line will produce every turn through the loop a query,
thats not good, how can i avoid that?

On Feb 28, 5:24 pm, LeonS [email protected] wrote:

Hi,

I’m trying to reduce my queries to the database so i use includes, but
there is produced a query every time and i can’t see an improvement.
What does i’m doing wrong?

It’s because you’re calling find_by_xxx which will always cause a
query.
you could instead do match.match_tips.detect {|m| m.user_id ==
current_user.id}

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs