简体   繁体   English

命名空间rails属居

[英]Namespace rails belongs_to

I have the following configuration 我有以下配置

module Account
   class Permission <ApplicationRecord
   end
end

module Account
   class GroupPermission < ApplicationRecord
     belongs_to: permission
   end
end

Table: account_permissions
          name character varying,
             
Table: account_group_permissions
        account_permissions_id bigint,
       account_groups_id bigint,

When I try to access the Account :: GroupPermission instance and through it access Account :: Permission returns nil. 当我尝试访问Account :: GroupPermission实例并通过它访问Account :: Permission时返回nil。

Only works if I specify class_name: 仅在我指定class_name时有效:

Is there a typo in your belongs_to ? 您的belongs_to有错别字吗? It should be belongs_to :permission 它应该是belongs_to :permission

Also, use permission_id as your foreign key in the account_group_permissions table. 此外,在account_group_permissions表中,将permission_id用作您的外键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM