我试图定义一个方法,该方法接受一个表示表名的可选参数,并将其传递给该方法中的SQL语句。当我在程序中运行代码时,我收到一条错误,说“#34;无法确定参数$ 1的数据类型。”#34;造成这种情况的原因是什么?如何解决?
这是我的代码。
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
提前致谢!
编辑:这是工作代码:
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