Hi!
I working in an application (developed in access over a oracle DB) on production server with all tables and fields that can't be changed. No table has an 'id' field for his primary key. I want to migrate this application to RoR, first in a development server, but i found some problems:
I run a scaffold to create one simple table, and this is my migration code: class CreateSafCarrecs < ActiveRecord::Migration def self.up create_table (:saf_carrecs, :primary_key => "idcarrec") do |t| t.column :desccarrec, :text end end def self.down drop_table :saf_carrecs end end
With this code, and running 'rake db:migrate', the table and the sequence for the autonumeric primary_key was created at the database. But when i want to insert a new row in the table through the new form generated with scaffold, it shows me this error:
OCIError: ORA-01400: cannot insert NULL into ("SAF_CARRECS"."IDCARREC"): INSERT INTO saf_carrecs (idcarrec, desccarrec) VALUES(NULL, empty_clob())
It seems like the primary_key was not generated automaticly.
I search for a solution and i modify my model like this: class SafCarrec < ActiveRecord::Base before_create :get_next_id def get_next_id id = ActiveRecord::Base.connection.select_value("select saf_carrecs_seq.nextval from dual") self.idcarrec = id end end
With this modification I consult the development.log and seems like the insert goes ok: 'INSERT INTO saf_carrecs (idcarrec, desccarrec) VALUES(10080, empty_clob())'
But just after this line in development.log, RoR make a select of the row by the 'id' field and show me this error in the browser: OCIError: ORA-00904: "ID": invalid identifier: SELECT desccarrec FROM saf_carrecs WHERE id = 10081
How can RoR make a select using 'id' field, if I changed it for 'idcarrec'? How can make it works?
Thanks, and sorry for my English.