I'm using activeadmin to manage the models of my rails app. I have a User model that uses the can can separate role model and those roles are modelled with inheritance and use STI on ActiveRecord.
The problem is that no matter on which of the roles' activeadmin controller page I'm, the index populated shows all the instances of Role the subclasses
Example:
I create RoleA and RoleB instances. Then I go to the RoleA index page and RoleB is shown in the list. The opposite happens as well.
The Details
I have several different roles which follow the role-object pattern where I have an abstract role and it's subclasses. I use this pattern because one User can have more than one role. On the other side, Roles share basic attributes but differ in some of them, that's why inheritance is use to model those roles
ROLE
|
---> RoleA
|
---> RoleB
|
---> RoleC
I have this migration for the STI
class CreateRoles < ActiveRecord::Migration
def change
create_table :roles do |t|
t.string :name #this is the name I want the role to show up on screen
t.references :role_a_attr
t.references :role_b_attr
t.string :type
t.timestamps
end
end
end
In my activeadmin controllers I have registered: Role, RoleA, RoleB and RoleC. Role
ActiveAdmin.register Role do
config.clear_action_items! # We don't want to create this kind of objects directly
index do
column :id
column :name
default_actions
end
end
RoleA
ActiveAdmin.register RoleA do
#we only want one super admin role
config.clear_action_items! if RoleA.first
menu :parent => 'Roles'
show do
attributes_table do
row :id
row :name
row :created_at
row :updated_at
end
end
end
RoleB
ActiveAdmin.register RoleB do
menu :parent => 'Roles'
end
RoleC
ActiveAdmin.register RoleC do
menu :parent => 'Roles'
end
What am I doing wrong?
Apparently ActiveAdmin does not like the default setting. Rails documentation advices to change the default name anyway, so I did by adding this to my migration file
class CreateRoles < ActiveRecord::Migration
def change
create_table :roles do |t|
# some other attributes
t.string :object_type #this will be your 'type' column from now on
t.timestamps
end
add_index :roles, :object_type
end
end
Then on the role class I added
set_inheritance_column 'object_type'
Surprisingly, this change was not getting any effect after doing a rake db:migrate. So I did a db:drop, db:reset, db:migrate and db:seed and everything started working fine.
Side note: Bare in mind that if you are doing a 'big bang' approach on development (you shouldn't) you can lock yourself out of the app when roles start working.