3

I need to insert a new record into an existing table in a databse. I tried both approaches below:

  class UserDetail < ActiveRecord::Base

 def self.add_new_user
   new_user = UserDetail.new
   new_user.first_name = 'Joe'
   new_user.last_name = 'Smith'
   new_user.user_id = 'TEST'
   new_user.save
 end

 def self.add_new_user_2

UserDetail.create(user_id: 'TEST', first_name: 'Joe', 
                  last_name: 'Smith')
 end

However, both approaches give me the error below:

    ActiveRecord::StatementInvalid: OCIError: ORA-00926: 
    missing VALUES keyword: INSERT INTO "USER_DETAIL" DEFAULT VALUES

What am I missing? Please share your solutions.

(Using Ruby 1.9.3, ActiveRecord 4.2.4)

2
  • What Oracle adapter are you using? Commented Nov 11, 2016 at 18:02
  • I am using: gem 'activerecord-oracle_enhanced-adapter', '1.6.7' Commented Nov 11, 2016 at 18:21

1 Answer 1

2

ActiveRecord makes assumptions about primary keys. It is expecting to find a primary key on your UserDetail table called "ID". Should look something like.

class UserDetail < ActiveRecord::Base
    self.table_name = :user_detail
    self.primary_key = :user_id

    def self.add_new_user
       new_user = UserDetail.new
       new_user.first_name = 'Joe'
       new_user.last_name = 'Smith'
       new_user.user_id = 1
       new_user.save
    end
end
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply! However, the USER_DETAIL table does not have a primary key, what it has is a constraint called **USER_EXPIRE_DT_UK **(USER_ID and EXPIRE_DT two columns in the USER_DETAIL table). How do I define this in ActiveRecord model class?
@user3314970 Changing the ID column is detailed in the AR docs: guides.rubyonrails.org/…

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.