Bazley
Bazley

Reputation: 2847

Rails testing error: argument of WHERE must be type boolean, not type integer

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

From this test:

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

EDIT:

Server logs:

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'

Upvotes: 3

Views: 5516

Answers (1)

vmarquet
vmarquet

Reputation: 2532

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

And, in postvoterelationships_controller.rb, I think:

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

should be:

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

Upvotes: 9

Related Questions