Rails测试错误:WHERE的参数必须是boolean类型,而不是类型integer

用户可以通过has_many关联投票给post。 我从运行测试中收到此错误:

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

 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] 

postvoterelationship.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 

user.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查询? 您可以在测试环境中显示它们,检查这个问题 。

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

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

应该:

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