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 <[email protected]> wrote: > @snusnu's plugin "accepts_nested_attributes" is probably the answer > but as a noobie I'm not having any luck getting it work. > (http://github.com/snusnu/dm-accepts_nested_attributes) > > Here's the scenario again: > > MODELS: > > class Trip > ... > has n, :trip_countries > has n, :countries, :through => :trip_countries > accepts_nested_attributes_for :countries > end > > class Country > ... > has n, :trip_countries > has n, :trips, :through => :trip_countries > accepts_nested_attributes_for :trips > end > > class TripCountry > property :id, Serial > belongs_to :trip > belongs_to :country > end > > In the trip VIEW form we have: > <% Country.all( :order=>[:name] ).each do |country| %> > <% checked = trip.countries.include?(country) ? 'checked' : nil %> > <%= check_box :name => "trip[countries][][id]", > :value => country.id, :checked => checked, > :boolean=>false, :label => country.name > %> > <% end %> > > I'm formatting the checkbox field names as "trip[countries][][id]" > but I'm not sure whether they should be > "trip[countries][]" or "trip[countries][id][]" etc?! > (Seehttp://wonderfullyflawed.com/2009/02/17/rails-forms-microformat) > Either way the checkbox selections will not save. > > Any help greatly appreciated! > Many many thanks, > George -- You received this message because you are subscribed to the Google Groups "merb" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/merb?hl=en.
