2

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"}

5
  • 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) Commented Jan 27, 2014 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.. Commented Jan 28, 2014 at 2:15
  • Actually, maybe its a rails 4 thing? Commented Jan 28, 2014 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' Commented Jan 28, 2014 at 8:01
  • 1
    Rails4 allows you to use find_by with a hash -> Article.find_by({name: "Hello"}) Commented Jan 28, 2014 at 10:21

1 Answer 1

4

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

2 Comments

Ah so I'm just missing the { } then? I will try this. Thank you Rich and Antoine
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.

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.