I've started a form that submits to a PostgresSQL database and I've done numerous tests on my models to verify my validation. However, I created a custom validator which checks a legacy Oracle database to verify that a license number provided in a form by an end-user exists in the legacy database. Via the command line debugger, I can verify that the library I created connects to the database and returns results and that the custom validator causes the model to be invalid if the criteria isn't met. However, when I run my unit tests against my model, an error occurs when the tests run. If I remove the custom validation, the tests run pass.
PG::Error: ERROR: relation "LICENSE" does not exist
LINE 4: WHERE a.attrelid = '"LICENSE"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"LICENSE"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
From what I see, it appears as though the tests are trying to run the custom validation against the PostgresSQL database specified in the database.yml file instead of the Oracle database that is specified in the library I wrote. I'm not quite sure if this is the cause or if it is why it is occurring and how I can resolve the issue.
Here is what my library file looks like:
module Legacy
DB_CONFIG = YAML::load(File.open("#{Rails.root}/config/legacy_database.yml"))[Rails.env]
class Legacy::License < ActiveRecord::Base
establish_connection DB_CONFIG
self.table_name = "LICENSE"
self.primary_key = "LICENSE_ID"
end
end
This is my model:
require "#{Rails.root}/lib/legacy.rb"
class Vehicle < ActiveRecord::Base
belongs_to :user
validates_presence_of :permit_license, :name
validate :validate_license
def validate_license
license = Legacy::License.where('license_number = ?', license_number)
if license.empty?
errors.add(:license_number, 'License not valid')
end
end
end
Any insight into why this is occurring would be helpful and appreciated.
I think it's the way you've configured the two databases. I have a similar situation, a Legacy database mixed with a Rails ActiveRecord based database. (SQLServer & mysql in my case)
Here is how I configure it. (development only shown):
config/database.yml:
aimdevelopment:
adapter: sqlserver
....
development:
adapter: mysql2
.....
My Legacy system is called AIM
Then I create a base model for ALL my Legacy Models to follow:
models/aim.rb:
class Aim < ActiveRecord::Base
establish_connection ("aim#{Rails.env}")
end
Then if I need to connect to table in the legacy database
class Product < AIM
self.table_name 'Product'
self.primary_key 'ProductID'
end
I Think the fact that you've circumvented rails when you tried to manage the 'establish_connection' within your module may be what is screwing you up.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.