When I try to display all the films in which a person is located, and they have more than 1 role (director, writer, actor) in the film, I get a few lines for this film. If I add .select ("DISTINCT id") or movies. * To try to eliminate duplicates, I get the following error:
Mysql2 :: Error: you have an error in the SQL syntax; check the manual that matches your version of MySQL server for the correct syntax to use next to DISTINCT id FROM moviesINNER JOIN movie_peopleON movies.id = movie_peop' at line 1: SELECTmovies .*, DISTINCT id FROMmovies INNER JOINmovie_people ONmovies .id =movie_people .movie_id WHERE ((movie_people`.person_id = 601619)) ORDER BY title LIMIT 18 OFFSET 0
I do not know how to correctly encode an isl request. Please, help.
Thank.
application / controllers
class PeopleController < ApplicationController
def show
@person = Person.find(params[:id])
@movies = @person.movies.select('DISTINCT id').
paginate :per_page => 18, :page => params[:page],
:order => sort_order(params[:sort])
end
application / models
class Person < ActiveRecord::Base
has_many :movie_people
has_many :movies, :through => :movie_people
end
class MoviePerson < ActiveRecord::Base
belongs_to :movie
belongs_to :person
end
class Movie < ActiveRecord::Base
has_many :movie_people
has_many :people, :through => :movie_people
end
db / schema.rb
create_table "people", :force => true do |t|
t.string "name"
end
create_table "movie_people", :force => true do |t|
t.integer "movie_id"
t.integer "person_id"
t.integer "role"
end
create_table "movies", :force => true do |t|
t.string "title"
t.string "year"
end
movies / show.html.erb
Title:<%= @movie.title %><br>
Year:<%= @movie.year %><br>
<% @movie.movie_people.group_by(&:role).each do |r, a| %>
<%= %w(Director: Writer: Cast:)[r] %>
<% a.each do |c| %>
<%= link_to c.person.name,
:controller => 'people', :action => 'show', :id => c.person.id %>
<% end %><br>
<% end %>
Title: Fahrenheit 9/11
Release year: 2004
Director: Michael Moore
Writer: Michael Moore
Cast: Michael Moore George W. Bush
people / show.html.erb
Name:<%= @person.name %>
<% @movies.each do |movie| %>
<br><%= link_to movie.title, movie %> (<%= movie.year %>)
<% end %>
:
9/11 (2004)
9/11 (2004)
9/11 (2004)