Any help with this would be very appreciated, I''m completely new to
rails. I''m building an application that''s looking to do the
following:
First off i''m trying to convert my site from ColdFusion to RoR, this is
the site: http://www.walkenclips.com/
It''s one page that basically does this:
Query the db to get a list of movies (movies table)
Loop through the movies and output each movie to the browser
Query the db within the loop to get files names of movie quotes from
that specific movie (movie_files table)
Loop through the movie quote names and output each quote to the browser
Continue to the next movie, rinse and repeat till the end of the loop
I''m able to display the the movies fine with the following code:
Controller:
class HomeController < ApplicationController
def index
@movie = Movie.find(:all, :order => "title")
end
end
Index.rhtml
<% for movies in @movie -%>
<div class="entry" >
<%= movies.title %>
</div>
<% end %>
movie.rb
class Movie < ActiveRecord::Base
end
While I''m looping through the results in the index.rhtml, how can I
tell
the controller I need the movie file names for that specific movie
within the "movie_files" table?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
in the controller @movie = Movie.find(:all, :order => "title") movie_filed = @movies.movie_files Off course, you need to define the model first: class MovieFile < ActiveRecord::Base belongs_to :movie end class Movie < ActiveRecord::Base has_many :movie_files end --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bill Walton wrote:> 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, > BillThanks for the replies guys, here is my table schema: movies table field - type - extra =======================id - int - auto increment (priamry key) title - varchar picture - varchar year - varchar rating - varchar played - varchar director - varchar movie_files table field - type - extra ========================id - int - auto increment movie_id - int name - varchar downloaded - int played - int -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Thanks for such a quick response Bill. Since I''m such a newbie to RoR I''m still a bit confused about method 1 you mentioned. How do I setup the hashes and have them interact with the view? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---