ruby - rails return tuples with common attributes -
i want display movies selected director. routes , controller work fine. however, filtered movies shown in view same. example, have 4 movies of 2 have same director. want show these 2 different tuples in view page, shown 2 tuples same. controller code:
def find_movies_by_same_director @movie = movie.find(params[:id]) @director = @movie.director if (not @director.nil?) , (not @director.empty?) #@movies = movie.find_all_by_director(@director) if (not @director.nil?) , (not @director.empty?); @movies = movie.find_by_sql("select * movies i.director == '#{@director}'") render :director else flash[:notice] = "'#{@movie.title}' has no director information" redirect_to root_path end end
i try both ways, find_by_sql , find_by_all, find tuples, both same results. view code:
%tbody - @movies.each |movie| %tr %th= @movie.title %th= @movie.rating %th= @movie.release_date
i'm new rails, comments or suggestion appreciated.
in view code, using instance variable @movie
, returns result of original search line 2 of controller code. see each movie iterate through @movies, need use local variable declaring in block.
%tbody - @movies.each |movie| %tr %th= movie.title %th= movie.rating %th= movie.release_date
if that's confusing, might change name of block variable entirely. doesn't change result might more readable.
%tbody - @movies.each |matched_movie| %tr %th= matched_movie.title %th= matched_movie.rating %th= matched_movie.release_date
edit: (it suggested add comment answer.)
this unrelated question, more standard rails way of performing search in line 6 @movies = movie.where(director: @director)
. better yet, since don't need use @director in view, do:
director = @movie.director @movies = movie.where(director: director)
Comments
Post a Comment