Question

I'm new to rails and still learning how it all works. I'm trying retrieve a list of years from a date column in an legacy Oracle database so I can get a list of all the years an object has been entered. The Oracle database is 10g and I'm using rails 3.2 with ruby 1.9.2p290 and using the oracle enhanced adapter gem.

Currently, I'm using the following code conduct my query (the .attribute is to help me determine what what is being returned from the query):

- License.select("distinct(to_char(application_date,'yyyy'))").each do |lic|
    %p= lic.attributes

The results I get back are a number of hashes containing all the distinct years found in the database column that I'm not sure how to access.

{"(to_char(application_date,'yyyy'))"=>"2002"}

I've also tried to simply selecting all rows from the date column from the database table and outputing the results. However, I get an Oracle error that I don't quite understand how to resolve.

- License.select("application_date").uniq.each do |lic|
    %p= lic.year

Give me the following error:

OCIError: ORA-01878: specified field not found in datetime or interval: SELECT DISTINCT application_date FROM "LICENSE" 

The Oracle Enhanced Adapter is configured to emulate dates by column name in the oracle initializer:

self.emulate_dates_by_column_name = true

I've also set the column in the License model to be a date column:

set_date_columns :application_date

Is there a way to retrieve the distinct years from all the rows in the date column in the database? I'm not quite sure on how to get the desired results of a dataset of unique years from that one column.

Was it helpful?

Solution

It appears that my issue had to do with the way Oracle handles it's date columns. By using the solution here, I was able to able to resolve my issue with the Oracle error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top