2015-06-16 39 views
2

用戶可以通過has_many關聯爲投遞投票。我從運行的測試收到此錯誤:Rails測試錯誤:WHERE的參數必須是類型布爾值,而不是類型整數

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

請提供一個堆棧跟蹤。 – jmdeldin

回答

7

您是否想看看做出的SQL查詢?您可以在測試環境中顯示它們,請檢查this question

而且,在postvoterelationships_controller.rb,我想:

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

應該是:

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

就是這樣。應該是'find',而不是'find_by'。我是一個半死。謝謝vmarquet。 – Bazley

+0

我有一個非常類似的錯誤。我確實想使用'find_by';在我的情況下,解決方案是更改爲:'.find_by(id:params [:id])'' –

相關問題