ActiveAdmin and AssociationTypeMismatch
I’ve spent a few good hours fighting a strange issue in my rails application’s ActiveAdmin module. I couldn’t create an has_many_through association without getting the AssociationTypeMismatch error on create or update of the resource!
Solutions found on the internet involved using elements_attributes instead of elements, adding accept_nested_attributes but nothing seemed to work! The solution turned out to be surprisingly simple.
Here is the example – the code is pretty trivial:
In a User model I have
class User < AR:Base has_many :groupings has_many :elelements, :through => :groupings end
In the ActiveAdmin view definition I have:
form do |f| f.inputs "Elements" do f.input :elements, :as => :check_boxes, :collection => Element.all end end
To get this to work you MUST add an active admin resource representing the connecting M2M table, in my case called Groupings. After adding it everything started working!
A small tip: Because u usually don’t want to have that table on your dashboard you can either hide it (set the menu option to false), or remove it from top menu: menu :parent => “Users” and it will show up as submenu under the users top menu. Nice, clean, and accessible 🙂
Hope it will save someone some time.
Happy Hacking in 2012