Reputation: 571
How can I display the extra attribute of the join model(in a has_many through association), in active_admin forms?
f.input :ad_slots
Will only display the ad_slots model, but I have a join table called stream_slots which has an extra column called duration. I want to be able to choose an ad_slot as well as enter the duration.
These are the models:
#live_stream:
has_many :stream_slots, :dependent => :destroy
has_many :ad_slots, :through => :stream_slots
#ad_slot:
has_many :stream_slots, :dependent => :destroy
has_many :live_streams, :through => :stream_slots
#stream_slot:
belongs_to :ad_slot
belongs_to :live_stream
and stream_slot has the ids of the other two models as well as an extra attribute called duration.
Thank you.
-- Tried something else--
Instead of linking to ad_slots, this is what I did:
f.has_many :stream_slots do |association|
association.inputs do
association.input :ad_slot
association.input :duration
end
end
and in LiveStream class, I added:
accepts_nested_attributes_for :stream_slots
This way the form shows the ad_slots for me to choose from, as well as a text field for the extra attribute, however, It fails when I try to save it. I believe I know what the problem is but I don't know how to solve it. It's because the live_stream_id in the stream_slots table is empty. How do set it to the newly created live_stream? (The one I'm creating now..)
Any help would be appreciated..
Upvotes: 0
Views: 2450
Reputation: 571
I got it working, As mentioned in the question the problem was that when the form was submitted, the live_stream_id was empty.
So I just removed the validation in the stream_slot model which says that the live_stream_id must be present and it worked.
Not sure if that's the best way to do it.. but it's working for now.
If anyone has a better solution, please share.
Upvotes: 1