3

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'
0

1 Answer 1

9

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
Sign up to request clarification or add additional context in comments.

2 Comments

That was it. Should have been find, not find_by. I am a halfwit. Thank you vmarquet.
I had a very similar bug. I did want to use find_by; in my case, the solution was to change to: .find_by(id: params[:id])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.