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’m using 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, Group is a resource by itself – an admin can manage groups by going to the /admin/groups route – but it also is a resource of users, and I’m able to view the user groups by accessing the /admin/users/:user_id/groups route. However, when I try to create a new group to this user by accessing the /admin/users/:user_id/groups/new page and filling in the form, I get an error:
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 logged in user and it is correct, while the group id = 13 corresponds to the id of the newly created group (it is saved in the database correctly), but the association in group_members table is not created. Actually, that’s not even exactly what I’m trying to achieve here: the ideal scenario would be to retrieve the list of groups and display it in the new page, so as not to create new groups in this page – only group_members.
How do I proceed in this case? Thanks in advance!
Have you considered removing the
declaration in the Group register block? The interface nesting that this aa dsl statement gives you, looks nice and gives you a clickpath, but, in the end it is merely a decoration on the data(model). Using filters and scopes in the user model could do as well. If you really need aa’s belongs_to you will have to create custom edit screens, and custom redirects. Good luck.