我有一个问题,我使用的查询finder_sql在移交给PostgreSQL之前没有正确解析导致数据库语法错误.
为了说明问题,我刚才使用了示例代码:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
我只改为class_name,"User"因为我没有人模型,但这在这里无关紧要.
has_many :subscribers, :class_name => "User", :finder_sql =>
'SELECT DISTINCT people.* ' +
'FROM people p, post_subscriptions ps ' +
'WHERE ps.post_id = #{id} AND ps.person_id = p.id ' +
'ORDER BY p.first_name'
Run Code Online (Sandbox Code Playgroud)
当我使用它时,我收到以下错误:
User Load (0.3ms) SELECT DISTINCT people.* FROM people p, post_subscriptions ps WHERE
ps.post_id = #{id} AND ps.person_id = p.id ORDER BY p.first_name
PGError: ERROR: Syntaxerror near »{«
LINE 1: ...ople p, post_subscriptions ps WHERE ps.post_id = #{id} …Run Code Online (Sandbox Code Playgroud) postgresql activerecord ruby-on-rails finder-sql ruby-on-rails-3