S
S
Sasha Grunin2014-04-23 20:40:42
Oracle
Sasha Grunin, 2014-04-23 20:40:42

Rails - association with custom foreign_key not working?

When I try to load the association, the model id is not passed to SQL, but is passed:

jruby-1.7.11 :081 > u.user_privileges
UserPrivilege Load (61.0ms)  
SELECT "A_USERPRIVILEGES".* FROM "A_USERPRIVILEGES"  
WHERE "A_USERPRIVILEGES"."ID_A_USERS" = :a1    =>
#<ActiveRecord::Associations::CollectionProxy []>

It has rails 4.1, jruby-1.7.11, oracle_enhanced adapter and oracle 11g as the database server.
Here is my code:
class User < ActiveRecord::Base
  self.table_name = 'A_USERS'
  self.primary_key = 'ID'

  has_many :user_privileges, foreign_key: 'ID_A_USERS', 
    primary_key: 'ID' # when i remove primary key here then nothing changing
end

class UserPrivilege < ActiveRecord::Base
  self.table_name = 'A_USERPRIVILEGES'
  self.primary_key = 'ID'

  belongs_to :user, foreign_key: 'ID_A_USERS', 
    primary_key: 'ID' # when i remove primary key here then nothing changing

  alias_attribute :user_id, :id_a_users # does not work too
end

Tables:
A_USERS:
    ID  NUMBER(38,0)

  A_USERPRIVILEGES:
    ID     NUMBER(38,0)
    ID_A_USERS NUMBER(38,0)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question