Rails Article.find 1 calls ActiveRecord :: StatementInvalid in the old database

I am creating a rails application on top of an old database table. Everything works fine locally, but on the server, I hit this error whenever I do Article.find (1)

Could not log "sql.active_record" event. NoMethodError: undefined method `name' for nil:NilClass ActiveRecord::StatementInvalid: PG::Error: ERROR: zero-length delimited identifier at or near """" LINE 1: SELECT "articles".* FROM "articles" WHERE "articles"."" = $1 LIMIT 1 

The legacy database has an id field, however the schema describes the identifier using

 create_table "articles", :id => false, :force => true do |t| t.decimal "id", :precision => 10, :scale => 0, :null => false ... 

Note that Article.find_by_id (1) returns the record without any problems.

Any ideas how to fix this?

+8
activerecord exception postgresql ruby-on-rails-3
source share
2 answers

UPDATED - see below

It turns out adding the following to the model fixes this:

 class Article < ActiveRecord::Base set_primary_key :id ... 

Presumably, since the rails did not create the table, she does not know in which field the primary key is located. It seems a little strange, however, an educated guess with a warning would be appreciated - or an even friendlier error message.

UPDATE: Rails 3.2+ the above method is deprecated. The new method is as follows:

 class Article < ActiveRecord::Base self.primary_key = :id ... 

Thanks to @lboix for pointing this out!

+19
source share

This option seems to be updated: set_primary_key error in Rails

Hope this helps :)

Take care

+4
source share

All Articles