ActiveAdmin / Formtastic sortable has_many through relationships

I might be missing something fundamental here, but I can't get ActiveAdmin to work with sorting has_many through relationships, with the ability to create new entries.

So, given the following models

class User < ActiveRecord::Base has_many :user_videos has_many :videos, through: :user_videos accepts_nested_attributes_for :user_videos accepts_nested_attributes_for :videos ... end class UserVideo < ActiveRecord::Base belongs_to :user belongs_to :video accepts_nested_attributes_for :video end class Video < ActiveRecord::Base has_many :user_videos has_many :users, through: :user_videos ... end 

(I admit that I am throwing accepts_nested_attributes_for around in the hope that something might work)

And setting up Active Admin goes something like this (WIP, of course):

 f.inputs "User" do f.has_many :user_videos, heading: 'Videos', sortable: :order, allow_destroy: true, new_record: 'New Record' do |v| v.inputs for: :video do |video| video.input :video_url end end f.has_many :videos, heading: 'Videos', new_record: 'New Video' do |v| v.input :video_url end end f.actions 

The first has_many in the association :user_videos does not represent any inputs. If there are entries there, I can see that video.input :video_url actually returns the li tag with label and input , however nothing is displayed on the page. For new entries, the entire v.inputs bit v.inputs not start (do I need to create child entries first?).

The second has_many will work so that you can add records and update existing records, but they cannot be sorted, since the order column is in the UserVideos model. I include this more as an illustration than anything.

If anyone has any pointers to this, they will be very grateful. :)

+5
source share
2 answers

Since no one was interested in this, I took a different approach β€” instead of getting ActiveAdmin / Formtastic to work with the existing model structure, I added getters and setters for the required field in the intersection model.

 class UserVideo < ActiveRecord::Base belongs_to :user belongs_to :video validates_with VideoValidator def video_url self.video = Video.create if video.nil? self.video.video_url end def video_url=(video_url) self.video = Video.create if video.nil? self.video.video_url = video_url # Video url is set via Active Admin, AA will not call save on the video as it does not realise it changed self.video.save! if video.present? and video.valid? end end 

This meant that Active Admin did not need to know about the video model and could just work with the UserVideo model:

  f.has_many :user_videos, heading: 'Videos', sortable: :order, allow_destroy: true, new_record: 'New Record' do |v| v.input :video_url, :hint => (v.object.video.embed_code unless v.object.nil? or v.object.video.nil?) end 

If someone has a real solution, not a job, I would like to hear it, but otherwise it is a possible solution for anyone who is looking for an answer to the same problem.

+2
source

Wow! I know I'm late to the party, but this is a great opportunity to use : delegation method !

The UserVideo class will look something like this:

 class UserVideo < ActiveRecord::Base belongs_to :user belongs_to :video validates_with VideoValidator delegate :video_url, :video_url=, to: :video end 

Good luck

+1
source

All Articles