Rails的破协会定制的IDS [英] Rails broken association with custom ids

查看:120
本文介绍了Rails的破协会定制的IDS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行自定义外键关系,护栏没有通过身份证查询,但通过[零,零]:

 的JRuby-1.7.11:081> u.user_privileges
UserPrivilege负荷(61.0ms)
选择A_USERPRIVILEGES。* FROMA_USERPRIVILEGES
在哪里办理A_USERPRIVILEGESID_A_USERS=:A1 [零,零] =>
#&所述; ActiveRecord的::协会:: CollectionProxy []≥
 

我有轨4.1,JRuby的-1.7.11,oracle_enhanced适配器和Oracle 11g数据库

下面是我的code:

 类用户的LT;的ActiveRecord :: Base的
  self.table_name ='A_USERS
  self.primary_key ='ID'

  的has_many:user_privileges,foreign_key:ID_A_USERS,
    primary_key:'ID'#,当我删除主键在这里再没有什么改变
结束

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

  belongs_to的:用户,foreign_key:ID_A_USERS,
    primary_key:'ID'#,当我删除主键在这里再没有什么改变

  alias_attribute:USER_ID,:id_a_users#不工作了
结束
 

表中的信息:

  A_USERS:
    ID号(38,0)

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

更新

我刚刚创建测试项目的的MySQL 的数据库完全相同code和模式作为writen以上,它的工作...

与Oracle的问题仍然没有解决。

解决方案

你有没有试过这种

 类用户的LT;的ActiveRecord :: Base的
  self.table_name ='A_USERS
  self.primary_key ='ID'

  的has_many:user_privileges,将class_name:UserPrivilege,foreign_key:ID_A_USERS
结束

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

  belongs_to的:用户,将class_name:用户,foreign_key:ID,primary_key:ID_A_USER
结束
 

foreign_key 是另一个表的键和 primary_key 的关键是当前表所以这是说看为 ID_A_USER A_USERS.ID

也在做类似这样的自定义表格的时候我总是喜欢告诉它Asssociation类可能是多余的,但对我来说似乎是有用的。

也请没有这个从这里

  

补充emulate_integers_by_column_name选项   在列的末尾总是会被模拟为Fixnum对象(有用的,如果在传统的数据库列类型指定就像数字,没有precision信息在默认情况下被映射到BigDecimal的红宝石设置下面,结果一些列的选项与ID型)

 的ActiveRecord :: ConnectionAdapters :: OracleEnhancedAdapter.emulate_integers_by_column_name =真
 

When I execute relation with custom foreign keys, rails does not pass id to query, but pass [[nil, nil]]:

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  [[nil, nil]]  =>
#<ActiveRecord::Associations::CollectionProxy []>

I have rails 4.1, jruby-1.7.11, oracle_enhanced adapter and oracle 11g db

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 information:

  A_USERS:
    ID  NUMBER(38,0)

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

Update

I just create test project with mysql database with exactly same code and schema as writen above, and it's working...

Problem with oracle still unsolved.

解决方案

Have you tried this

class User < ActiveRecord::Base
  self.table_name = 'A_USERS'
  self.primary_key = 'ID'

  has_many :user_privileges, class_name: 'UserPrivilege', foreign_key: 'ID_A_USERS'
end

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

  belongs_to :user, class_name:'User', foreign_key: 'ID', primary_key: 'ID_A_USER'
end

foreign_key is the key in the other table and primary_key is the key the current table so this is saying look for ID_A_USER in A_USERS.ID

Also when doing custom tables like this I always like to tell it the Asssociation class might be redundant but seems useful to me.

Also please not this from here:

Added emulate_integers_by_column_name option Set the option below and as a result number columns with ID at the end of column always will be emulated as Fixnum (useful if in legacy database column type is specified just as NUMBER without precision information which by default is mapped to BigDecimal Ruby type)

ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_integers_by_column_name = true

这篇关于Rails的破协会定制的IDS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆