Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I moved my app from sqlite to postgresql so that I could use RGeo and PostGIS. I deleted all the data in my database and started from scratch. Everything is working fine except one particular area of the site. I have an association that links Tourstops and Venues that appears to be not working. No code was modified in this area since the switch. Unfortunately I'm still sort of new to rails and don't know enough to diagnose this error.

The error I'm getting is:

ActiveRecord::StatementInvalid in ToursController#show

PG::DatatypeMismatch: ERROR: argument of WHERE must be type boolean, not type integer LINE 1: SELECT "venues".* FROM "venues" WHERE (1) LIMIT 1 ^ : SELECT "venues".* FROM "venues" WHERE (1) LIMIT 1

  def show
    @tour = Tour.find(params[:id])
    @tourstop = Venue.find_by(params[:id])
  end

Parameters: {"id"=>"1"}

share|improve this question
4  
Maybe it's just a typo, but I don't think find_by is an actual ActiveRecord method (at least not with just an id as a parameter) – Antoine Jan 27 '14 at 15:25
    
I don't really know enough to be sure, but it was working before the switch. Also in the rails docs it has find_by here - api.rubyonrails.org/classes/ActiveRecord/… :-/ Not sure why I used find_by here to begin with though.. – user3155441 Jan 28 '14 at 2:15
    
Actually, maybe its a rails 4 thing? – user3155441 Jan 28 '14 at 4:27
    
Yes, but you need to pass the name of the column as the first parameter (id here I guess), or try with just 'find' – Antoine Jan 28 '14 at 8:01
1  
Rails4 allows you to use find_by with a hash -> Article.find_by({name: "Hello"}) – Rich Peck Jan 28 '14 at 10:21

1 Answer 1

up vote 3 down vote accepted

Your problem, as stated in the comments, that you're using find_by without any :key in the hash

To be clear, ActiveRecord should be able to translate between SQLite & PGSQL, so the difference has to be with your use of the ActiveRecord methods:


find

Finds individual element by primary_key:

Model.find(id)

find_by

Finds by value other than primary_key:

Model.find_by name: "Rich"
Model.find_by({name: "Rich"})
share|improve this answer
    
Ah so I'm just missing the { } then? I will try this. Thank you Rich and Antoine – user3155441 Jan 28 '14 at 15:13
1  
Actually, had this in several places. Most places didn't have Venue.find_by(params[:id]) but Venue.find_by(name: value) instead. Adding the {} to those worked and converting the others to find(params[:id]) was the rest of the solution. I'm still curious why it worked before though. – user3155441 Jan 28 '14 at 16:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.