Reputation: 175
Has anyone experienced this issue on mapping table in ActiveRecord when table name need a schema name as a prefix (oracle)?
Gemfile
gem 'activerecord', '4.2.4'
gem 'activerecord-oracle_enhanced-adapter', '1.6.7'
....
db_helper.rb
class Student < ActiveRecord::Base
self.abstract_class = true
self.table_name_prefix = 'OPT_ABC.'
self.table_name = 'STUDENT'
def self.list_student
puts Student.take(1) #testing
end
end
The actual table name looks like:
SELECT * FROM OPT_ABC.STUDENT;
I am able to connect to the database instance, but when the code gets line:
puts Student.take(1) # SELECT * FROM STUDENT LIMIT 1
I get the following error:
ActiveRecord::StatementInvalid:
table or view does not exist: SELECT "STUDENT".* FROM "STUDENT"
I am looking for solution on how to handle 'OPT_ABC." table prefix. Please share your solution.
Upvotes: 3
Views: 1941
Reputation: 2840
It looks like the problem is that you're trying to use both self.table_name_prefix=
and self.table_name=
together when you should be using one OR the other.
First let's consider how both self.table_name_prefix=
and self.table_name=
work.
According to the documentation, self.table_name_prefix=
works by prepending the passed in value to the table name that ActiveRecord automatically generates based off of the name of the class.
So, if the class name is Student
and you do self.table_name_prefix = 'OPT_ABC.'
, your table name will be OPT_ABC.STUDENTS
. Note that the generated table name is plural (and ends in an s
).
According to the documentation, self.table_name=
sets the table name explicitly. This means that it completely overrides the table name to the value that you pass in.
So, if you do self.table_name = 'OPT_ABC.STUDENT'
your table name will be OPT_ABC.STUDENT
.
So, given that, to set the table name to OPT_ABC.STUDENT
, you should be able to simply pass the value into self.table_name
like this:
class Student < ActiveRecord::Base
self.abstract_class = true
self.table_name = 'OPT_ABC.STUDENT'
def self.list_student
puts Student.take(1) #testing
end
end
Upvotes: 6