triggering a query within a loop

Hi Bill

Bill wrote:

How can I tell the controller I need the movie
file names for that specific movie within the
"movie_files" table?

In general, you're going to do a find on the movie_files table for each movie. What the find looks like depends on what your movie_files table schema looks like.

The other thing you'll need to address is the data structure to pass to your view. My personal preference would probably be to construct a hash of hashes in the controller, but there are other ways. Share some more info and we can probably be of more help.

Best regards,
Bill

Hi Frank,

Is movie_id declared as a foreign key? It should be. Once it is, you can do something like...

movies = Movie.find(:all)
movies.each {|this_movie|
  clips = MovieFile.find_all_by_movie_id(this_movie.id)
}

As I noted before, however, you're going to need to decide how you want to structure this data for use in your views. IMHO, there are three obvious approaches. 1) You could construct a hash of hashes. 2) You could name each of the clips arrays so they're 'associated' with some field in your movies records. Either of these would work with your existing model. The hashes approach is cleaner (again IMHO) and there are, of course, other approaches. The third option is the easiest. 3) De-normalize your data.

Lest you want to invite the displeasure of the RoRing Gods, the one thing you don't want to do (although it's certainly possible) is to do the clips find in the view.

hth,
Bill

Hi Frank,

Sorry for my delay in responding. I was out of the office all afternoon yesterday. Will be again today so please don't take offense if you have a follow-on that I don't respond to til tomorrow.

I took another look at the models you posted and think perhaps I've misunderstood your needs. My initial understanding was that you had multiple quotes, _and_ a clip associated with each one, that you wanted to output for each movie. In rereading your description, it seems that you only want to output the quotes. If that's true, then you don't need a hash of hashes. A hash of arrays will do fine.

If my (new) understanding is correct, in an action in your controller...

@movie_quotes = Hash.new
@movies = Movie.find(:all)
@movies.each {|this_movie|
  @movie_quotes[this_movie] = MovieFile.find_all_by_movie_id(this_movie.id)
}

In your view...

<% @movie_quotes.each do |movie_name, quotes| %>
  <%=h( movie_name )%>
  <% quotes.each do |this_quote| %>
      <%=h( this_quote )%>
  <% end %>
<% end %>

hth,
Bill