:has_many Relationships in ActiveAdmin
When I began using ActiveAdmin I discovered that there is not a lot of documentation on advanced usage such as displaying the :has_many relationships on the index and show page in the admin of the posts. I wanted to make it easy to add categories to posts and to know how posts were categorized easily.
Once the Category and Post models are created a join model also needs to exist, in this case Categorization. This part is the same as any other :has_many
, the hard part is display in the admin.
Go into the admin view for Post and add the following (other fields removed for easy viewing):
As you can see inside the tables creating a special inner table is required for best display. I add the link to the category edit in so it's easy to edit/view the category from the posts table.
To add checkboxes for category select it's as simple as adding f.input :categories, :as => :check_boxes
to your form view.
Want to figure out how to set up active admin? Read my "Lots of Love for ActiveAdmin" post. If you have questions, comment on the gist on github or find me on twitter.
ActiveAdmin is a really great simple way to implement an admin for a simple website or blog.