2015-04-16 41 views
3

我试图创建一个唯一的患者谁留下了评论,以病人谁留下最新的评论第一的名单。ProtocolViolation:错误:绑定消息提供0参数,但准备语句“”需要1

这是我的Ruby .erb代码来创建列表:

@comment_list.order("created_at desc").each_with_index do |comment, index| 

@comment_list在控制器中定义为:

@comments = current_clinician.comments.select('ON (patient_id) *').uniq 
    @comments = @comments.order("patient_id, created_at DESC") 
    @comment_list = Comment.select('*').from("(#{@comments.to_sql}) sub") 

我得到一个ActiveRecord :: StatementInvalid消息:

PG::ProtocolViolation: ERROR: bind message supplies 0 parameters, but prepared statement "" requires 1 : SELECT * FROM (SELECT DISTINCT ON (patient_id) * FROM "comments" WHERE "comments"."clinician_id" = $1 ORDER BY patient_id, created_at DESC) sub ORDER BY created_at desc

我试图按照24619117上的答案,我的输出是一个组合这和答案顶部29660396

$ rails -v 
Rails 4.1.8 
$ ruby -v 
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14] 
$ psql --version 
psql (PostgreSQL) 9.4.1 

我初出茅庐与PostgreSQL的,问题的部分是,我使用Ruby on Rails的获得SQL和方法并不简单。我一直在使用http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-from

建议,请

回答

1

你的情况,似乎是因为你使用的是@comments.to_sql你拉的是事先准备好的声明到您的子查询没有带来它的参数。你可以尝试只包括像这样的注解数据:

@comments = current_clinician.comments.select('ON (patient_id) *').uniq.order("patient_id, created_at DESC").include(:comment) 
    @comment_list = @comments.include(:comment) 

这一问题也似乎都来自于该准备的语句是建立在Rails和可以通过内滑轨本身(Rails的问题#15920要么问题引起的方式,在Rails 4.2中已经修复)或者通过各种帮助生成查询的gem的问题(例如:Rails问题#20236)或甚至通过您定义模型关联的方式(Rails问题#12852)。

它可能只是直接通过添加指令您database.yml文件中禁用准备语句:

production: 
    adapter: postgresql 
    database: prod_dbname 
    username: prod_user 
    password: prod_pass 
    prepared_statements: false 

但首先,你可能要检查并确保你是不是在你的模型中使用不必要的参数这样的协会:

class DashboardTab < ActiveRecord::Base 
    has_many :dashboard_tab_feeds, foreign_key: :dashboard_tab_id, dependent: :destroy 
    has_many :social_feeds, through: :dashboard_tab_feeds 
end 

class DashboardTabFeed < ActiveRecord::Base 
    belongs_to :social_feed 
    belongs_to :dashboard_tab 
end 

class SocialFeed < ActiveRecord::Base 
    has_many :dashboard_tab_feeds, foreign_key: :social_feed_id, dependent: :destroy 
end 

...这应该只是离开了foreign_key,像这样:

class DashboardTab < ActiveRecord::Base 
    has_many :dashboard_tab_feeds, dependent: :destroy 
    has_many :social_feeds, through: :dashboard_tab_feeds 
end 

class DashboardTabFeed < ActiveRecord::Base 
    belongs_to :social_feed 
    belongs_to :dashboard_tab 
end 

class SocialFeed < ActiveRecord::Base 
    has_many :dashboard_tab_feeds, dependent: :destroy 
end 
相关问题