Rails 5 belongs_to namespace

Asked

Viewed 45 times

1

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 access it Account::Permission returns nil.

Only works if I specify the class_name:

1 answer

0

It is not common, in Rails, to put models inside modules, as you did in your models with the module Account.

So Rails can’t find the association class automatically, so it only works by specifying a class_name.

belongs_to :permission, class_name: "Account::Permission"

I advise you to remove the modules as Rails follows conventions on configurations. That means you may have more problems later.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.