Ok I've now solved this myself but I suspect there may be a better
way!
(My problems were simply due to my lack of understanding of merb and
the way arrays of fields should be handled)
It seems that while the accespt_nested_attributes plugin (http://
github.com/snusnu/dm-accepts_nested_attributes) can help with changing
the actual values of nested fields, it cannot help with adding and
removing them.
Hence when we have a bunch of checkboxes representing a has-n-through
association, our model needs to have a "relations_ids=" method.
(Better explanation here:
http://asciicasts.com/episodes/17-habtm-checkboxes)
So, looking at the "trip has n countries" relationship described in my
earlier post, if we render several Country checkboxes like this:
<input type="checkbox" name="trip[countries_ids][]" value="587"/
><label>Algeria</label>
...then we must define a method in the Trip model to receive an array
of countries_ids, like this:
def countries_ids=(ids)
self.countries.reject!{|model| !ids.include?(
model.id) }
self.countries.concat Country.all( :id => ids )
end
This handles all the creating and deleting of counties in the
trip_countries table.
(Notice also that the controller's update action will need to allow
for when all checkboxes have been un-ticked because the posted data
will not include an mention of the countries_ids array. Hence we add
the line "trip[:countries_ids] ||= []" before "if
@trip.update_attributes(trip)"
I wasted a lot of time on this so I hope this info helps someone.
Maybe an experienced rails/merber can suggest a better way, perhaps
one that does not require custom code on the model.
Cheers,
George
On Dec 8, 12:58 pm, George Adamson <George.Adam...@SoftwareUnity.com>
wrote: