PostgreSQL and Heroku, find and group - ruby-on-rails

I'm trying to get an app to run on Heroku properly. (Heroku uses the postgreSQL database, yeh?)
In development, I'm using sqlite, and this is my code in a controller =>
#productsort = Products.find(:all,
:select => 'count(*) count, color',
:group => 'color',
:order => 'count DESC',
:conditions => 'size = "Small"')
As you can see, I'm trying to group products by their colors, and order them by greatest amount to least.
Also, the products must be "Small". (the conditions)
In SQL, it works fine.
But not in PostgreSQL (heroku).
This is from running "heroku log"
2011-06-20T18:20:33+00:00 app[web.1]: ActiveRecord::StatementInvalid (PGError: ERROR: column "Small" does not exist
2011-06-20T18:20:33+00:00 app[web.1]: LINE 1: ...ducts".* FROM "products" WHERE (size = "Smal...
Hm... I've searched around and I couldn't find anything similar to what I have.
All help would be appreicated. Thank you

You need to be using single quotes around your strings in the conditions (double quotes may work with sqlite, but they definitely don't with PostgreSQL).
So replace your conditions with this:
:conditions => "size = 'Small'"
It will still work in SQLite too.

Related

rails 3.2.1 and mysql where condition with nil issue (it toggles in no record and proper result)

If I run the following query in rails console
On first run it returns 7 rows and 2nd run it returns 0 rows, its keeps toggling...
#site.site_pages.published\
.where(:is_visible => true,:parent_id => nil,:lang_parent_id => nil)\
.order("position ASC")
=> [ARRAY OF RECORDS]
-- {Returns 7 rows}
#site.site_pages.published\
.where(:is_visible => true,:parent_id => nil,:lang_parent_id => nil)\
.order("position ASC")
=> []
-- {Returns 0 rows}
As a fix/work around I tried this, which worked fine, but need to know the root cause
#site.site_pages.published\
.where(:is_visible => true,:parent_id => [nil, ''],:lang_parent_id => [nil, ''])\
.order("position ASC")
NOTE: If i run the to_sql query in mysql, it works fine, so considering nothing to do with mysql version etc, but its 5.6.

Full-text search on Heroku using pg_search gem

I've implemented full-text search using pg_search gem for my Rails application
My migration to create index looks like
execute(<<-'eosql'.strip)
CREATE index mytable_fts_idx
ON mytable
USING gin(
(setweight(to_tsvector('english', coalesce("mytable"."name", '')), 'A') ||
' ' ||
setweight(to_tsvector('english', coalesce("mytable"."description",'')), 'B')
)
)
eosql
And my controller code looks like
pg_search_scope :full_text_search,
:against => [
:name, :description],
:using => {
:tsearch => {
:prefix => true,
:dictionary => "english",
:any_word => true
}
}
which works totally fine locally on Postgres 9.0.4. However, when I deploy the same to heroku and search for a sample query 'test', it throws up an error
PGError: ERROR: syntax error in tsquery: "' test ':*"
SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "mytable" WHERE (((to_tsvector('english', coalesce("mytable"."name", '')) || to_tsvector('english', coalesce("mytable"."description", ''))) ## (to_tsquery('english', ''' ' || 'test' || ' ''' || ':*')))) LIMIT 12 OFFSET 0) subquery_for_count ):
Any suggestions on where I'm wrong and what I should be looking at to fix this error? Thanks.
I'm the main developer of pg_search. Sorry that you ran into that problem! Right now there is a pg_search bug when using :prefix searches against PostgreSQL 8.3 (the default for Heroku).
https://github.com/Casecommons/pg_search/issues/10
It's my top priority right now. I'm still figuring out the best way to get the test suite to run against both 8.x and 9.x.
Update: Unfortunately, :prefix searches don't work against PostgreSQL 8.3 at all. The functionality was introduced in 8.4. I've released pg_search 0.3.3 which improves the error message. Hopefully Heroku will upgrade to 9.0 across the board soon. I believe they want to do so, but they obviously can't just upgrade everyone wholesale without warning.

Thinking Sphinx and Norwegian characters (æ, ø, å)

I've set up Thinking Sphinx for wildcard searches, but I'm having trouble searching for words containing Norwegian characters, as the automatic starring seems to mess up the query. For instance, my search for "ål" will end up with:
Sphinx Query (2.8ms) å*l*
Sphinx Found 0 results
If I manually enter the stars in the search term, "*ål*", the expected results are returned:
Sphinx Query (3.7ms) *ål*
Sphinx Found 8 results
It seems somehow the å (as well as æ, ø) gets misinterpreted when automatically adding the stars.
Anyone here familiar with this problem?
My config/sphinx.yml looks as follows:
development:
enable_star: 1
min_infix_len: 2
charset_table: "U+FF10..U+FF19->0..9, U+FF21..U+FF3A->a..z, U+FF41..U+FF5A->a..z, 0..9, A..Z->a..z, a..z,
U+C5->U+E5, U+E5, U+D8->U+F8, U+F8, U+C6->U+E6, U+E6,
U+C4->U+E4, U+E4, U+D6->U+F6, U+F6"
And a couple of examples of searches performed in the console:
ruby-1.9.2-p290 :014 > ThinkingSphinx.search("ål", :star => true).count
=> 0
ruby-1.9.2-p290 :015 > ThinkingSphinx.search("*ål*", :star => true).count
=> 8
This has been fixed in recent commits - for the moment, you'll need to grab it via the repo:
gem 'thinking-sphinx',
:git => 'git://github.com/freelancing-god/thinking-sphinx.git'

Rails Location based search not working on Heroku

Recently I deployed my application on Heroku. I'm using Geokit to search events, based on user's location.
#events = Event.all(:origin => [#location.lat, #location.lng], :within => 5, :conditions => ["end_date >= ?", Date.today], :select => "id, created_at, user_id, location, description, permalink, title", :order => "created_at DESC")
The above statement is working fine in my local system with mysql database.
But I'm getting error while executing same stmt on Heroku. Please check the below error I'm facing on Heroku.
ActiveRecord::StatementInvalid (PGError: ERROR: function radians(character varying) does not exist
2011-03-20T00:52:23-07:00 app[web.1]: LINE 2: ...,COS(0.303256183987648)*COS(1.36963671754269)*COS(RADIANS(fr...
2011-03-20T00:52:23-07:00 app[web.1]: ^
2011-03-20T00:52:23-07:00 app[web.1]: HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Any help would be greatly appreciated.
Thanks,
Kalyan.
As a heads-up, Heroku uses Postgres as the only SQL database backend: http://devcenter.heroku.com/articles/database
Therefore, your problem is related to these two Q&A:
Why does Postgresql fail with Geokit like this?
Rails: Converting from MySQL to PostGres breaks Geokit Distance Calculations?
In your case, you should add a database migration and change "lat" and "lng" in your location model from "string" (or "text") to "float", and then Geokit should work fine with Postgres and Heroku respectively.

Why am I getting a PG error on heroku when it works fine on my local rails environment?

I am getting the following error:
ReportsController#return_search (ActiveRecord::StatementInvalid) "PGError: ERROR: syntax error at or near \"FROM\"\nLINE 5: ...OUNT(contact_postalcards.id) AS postalcard_count, FROM \"cont...\n
It works fine locally, but when I push to heroku I get the error:
What do I need to change so it will work also on Postgres on Heroku?
#list = Contact.find :all,
:select => "companies.name AS co_name,
companies.id AS comp_id,
COUNT(contact_emails.id) AS email_count,
COUNT(contact_calls.id) AS call_count,
COUNT(contact_letters.id) AS letter_count,
COUNT(contact_postalcards.id) AS postalcard_count",
:conditions => ['contact_emails.date_sent < ? and contact_emails.date_sent > ?',
report_end_date, report_start_date],
:joins => [
"LEFT JOIN companies ON companies.id = contacts.company_id",
"LEFT JOIN contact_emails ON contact_emails.contact_id = contacts.id",
"LEFT JOIN contact_letters ON contact_letters.contact_id = contacts.id",
"LEFT JOIN contact_postalcards ON contact_postalcards.contact_id = contacts.id",
"LEFT JOIN contact_calls ON contact_calls.contact_id = contacts.id"
],
#:group => "companies.id"
:group => "companies.name"
puts #list[0].attributes.inspect
From the errormessage:
AS postalcard_count, FROM
Drop this comma , in your query, it's your bug.
It looks like this was a PG specific so I changed the database structure altogether.

Resources