Rails 测试错误:WHERE 的参数必须是布尔类型,而不是整数类型 [英] Rails testing error: argument of WHERE must be type boolean, not type integer

查看:90
本文介绍了Rails 测试错误:WHERE 的参数必须是布尔类型,而不是整数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户可以通过 has_many 关联为帖子投票.我在运行测试时收到此错误:

Users can vote for Posts through a has_many association. I'm getting this error from running tests:

ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR: argument of WHERE must be type boolean, not type integer

来自这个测试:

test "should unvote a post the standard way" do
  @user.vote(@post_2)
  postvoterelationship = @user.active_post_vote_relationships.find_by(voted_id: @post_2.id)
  assert_difference '@user.postvoting.count', -1 do
    delete postvoterelationship_path(postvoterelationship)
  end
end

postvoterelationships_controller.rb

postvoterelationships_controller.rb

def destroy
  @user = Postvoterelationship.find(params[:id]).voter
  @post = Postvoterelationship.find_by(params[:id]).voted
  @user.unvote(@post)
  respond_to do |format|
    format.html do
      redirect_to @user
    end
    format.js
  end
end # destroy

routes.rb

resources :postvoterelationships,  only: [:create, :destroy]

投票后关系.rb

class Postvoterelationship < ActiveRecord::Base
  belongs_to :voter, class_name: "User"
  belongs_to :voted, class_name: "Post"
  validates  :voter_id, presence: true
  validates  :voted_id, presence: true
end

用户.rb

has_many :postvoting, through: :active_post_vote_relationships, source: :voted

服务器日志:

Started DELETE "/postvoterelationships/207" for ::1 at 2015-06-16 21:02:07 +0100
Processing by PostvoterelationshipsController#destroy as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bP17sWN2k6lFqnNxEa6NNEO5yWXkGopi9PSXLIEKzooR3XUfF4chhS3+W+9WP8EB3GwzfhsauAyPiIp1/kkh9A==", "commit"=>"Unvote", "id"=>"207"}
Character Load (0.3ms)  SELECT  "characters".* FROM "characters" WHERE "characters"."callsign" = $1 LIMIT 1  [["callsign", "bazzer"]]
User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
Postvoterelationship Load (1.2ms)  SELECT  "postvoterelationships".* FROM "postvoterelationships" WHERE "postvoterelationships"."id" = $1 LIMIT 1  [["id", 207]]
Character Load (0.7ms)  SELECT  "characters".* FROM "characters" WHERE "characters"."id" = $1 LIMIT 1  [["id", 1]]
Postvoterelationship Load (1.8ms)  SELECT  "postvoterelationships".* FROM "postvoterelationships" WHERE (207) LIMIT 1
PG::DatatypeMismatch: ERROR:  argument of WHERE must be type boolean, not type integer
LINE 1: ...ationships".* FROM "postvoterelationships" WHERE (207) LIMIT...
                                                         ^
: SELECT  "postvoterelationships".* FROM "postvoterelationships" WHERE (207) LIMIT 1
Completed 500 Internal Server Error in 14ms (ActiveRecord: 4.3ms)

ActiveRecord::StatementInvalid (PG::DatatypeMismatch: ERROR:  argument of WHERE must be type boolean, not type integer
LINE 1: ...ationships".* FROM "postvoterelationships" WHERE (207) LIMIT...
                                                         ^
: SELECT  "postvoterelationships".* FROM "postvoterelationships" WHERE (207) LIMIT 1):
app/controllers/postvoterelationships_controller.rb:24:in `destroy'

推荐答案

您是否尝试查看所做的 SQL 查询?您可以在测试环境中显示它们,请检查 this问题.

Have you tried to look at the SQL queries made? You can show them in test environment, check this question.

而且,在 postvoterelationships_controller.rb 中,我认为:

And, in postvoterelationships_controller.rb, I think:

  @post = Postvoterelationship.find_by(params[:id]).voted

应该是:

  @post = Postvoterelationship.find(params[:id]).voted

这篇关于Rails 测试错误:WHERE 的参数必须是布尔类型,而不是整数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆