Nested Active Admin Associations

So, I have the following models in my program:

class User < ActiveRecord::Base has_many :group_members has_many :groups, through: :group_members class GroupMember < ActiveRecord::Base belongs_to :user belongs_to :group end class Group < ActiveRecord::Base has_many :group_members has_many :users, through: :group_members end 

And I use ActiveAdmin to administer these resources. My groups.rb file in the admin folder looks like this:

 ActiveAdmin.register Group do . . belongs_to :user, :optional => true . . end 

The problem is that Group is a resource in itself - an administrator can manage groups by going to the /admin/groups route, but it is also a user resource, and I can browse user groups by accessing /admin/users/:user_id/groups route. However, when I try to create a new group for this user, by accessing the /admin/users/:user_id/groups/new page and filling out the form, I get an error message:

 ActiveRecord::RecordNotFound in Admin::GroupsController#show Couldn't find Group with id=13 [WHERE `group_members`.`user_id` = 2] 

The user ID corresponds to my currently registered user, and this is correct, while the group identifier = 13 corresponds to the identifier of the newly created group (it is correctly stored in the database), but the association in the group_members table is not created. Actually, this is not even what I am trying to achieve here: the ideal scenario would be to get a list of groups and display it on a new page so as not to create new groups on this page - only group_members .

How can I continue in this case? Thanks in advance!

+4
source share
1 answer

Have you considered the removal

 belongs_to 

in the group register block? The interface embedded in this aa dsl operator gives you looks beautiful and gives you a mouse click, but, in the end, it's just a decoration of the data (model). Using filters and areas in a user model might as well. If you really need aa belongs_to, you will have to create custom editing screens and custom redirects. Good luck.

+4
source

Source: https://habr.com/ru/post/1443452/


All Articles