I'm trying to define a method that takes an optional argument representing a table name and passes it to a SQL statement within that method. When I run the code in my program I get an error saying "could not determine data type of parameter $1." What is causing this and how can I solve it?
Here is my code.
def get_all_movies(order='title')
sql = %{
SELECT movies.title AS title, movies.year AS year, movies.rating AS rating,
genres.name AS genre, studios.name AS studio
FROM movies
JOIN genres ON movies.genre_id = genres.id
JOIN studios ON movies.studio_id = studios.id
ORDER BY movies.#{order};
}
db_connection do |conn|
order = conn.quote_ident(order)
@results = conn.exec_params(sql, [order]).to_a
end
@get_all_movies = true
end
Thanks in advance!
EDIT: Here is the working code:
def get_all_movies(order='title')
sql = %{
SELECT movies.title AS title, movies.year AS year, movies.rating AS rating,
genres.name AS genre, studios.name AS studio
FROM movies
JOIN genres ON movies.genre_id = genres.id
JOIN studios ON movies.studio_id = studios.id
ORDER BY movies.#{order};
}
db_connection do |conn|
order = conn.quote_ident(order)
@results = conn.exec(sql).to_a
end
@get_all_movies = true
end