Topic: RESTful bulk has_and_belongs_to_many

My users shall be able to tell the system about their language skills. This is modeled by using a first class association object 'language_skill' that associates the proband with a language.

So there's...

class Proband < ActiveRecord::Base
  has_many  :language_skills

class LanguageSkill < ActiveRecord::Base
  belongs_to  :proband
  belongs_to  :language
class Language < ActiveRecord::Base
  has_many   :language_skills

It would be unusable to do this in the 'scaffold-way' (separate pages for list/add/edit). Instead I want to make an edit page that lists all languages with checkboxes where the user can select/deselect the language as his skill.

As I've choosen to go the RESTful way, I ask myself how to fit this into the RESTful design pattern. Currently I have a LanguageSkillController with just one method('edit'/'update') that displays the languages and the checkboxes at once and handles updates. So I see the LanguageSkill as a single resource (and not as a collection of association objects).

Does this make sense ?
Are there any alternatives ?

Re: RESTful bulk has_and_belongs_to_many

You can easily do this with a HABTM association but it's harder with a has_many :through association where there is a full join model. See this thread for details.

As far as how this fits into REST, IMO it should be all in the ProbandsController#edit action. You can edit the checkboxes along with all the other proband fields here.

Railscasts - Free Ruby on Rails Screencasts

Re: RESTful bulk has_and_belongs_to_many

I modeled it with an association object because I need additional info on the skills (skill level). HABTM would not allow this.

Regarding RESTful integration: the problem when I put this into proband's edit action is that all the skills stuff is handled on it's own page. In fact I have many objects that are linked to probands. The proband itself can be edited in different ways. Thus I implemented RESTful controllers for all these 'aspects', even if these aren't backed by their own persistent objects, but in fact by parts of the proband itself. Until now I only had 'virtual' 1:1 associations (proband:parts of proband), but now I have a 1:n (proband to skills) which confuses me and doesn't fit well into RESTful design.

Re: RESTful bulk has_and_belongs_to_many

Since there's additional information in the LanguageSkill model then checkboxes probably won't work for you. Instead see if this will work:

On the Proband show page, list the current languages/language skills and have an "add language" or "new language skill" button on this page. This will go to the "new" action of the LanguageSkills controller passing the proband_id to it so it knows which proband the skill is for. You can have a normal model form for setting the skill's attributes (you can use collection_select for setting the language). Then when you save it you can have it go back to the proband show page.

Then in the list of skills you can have an "edit" link next to each one which goes back to the LanguageSkills controller's edit action which is just like editing a model again.

Do you understand? Will this work for you?

I would call this approach a "pure" RESTful way to handle it. If the interface is too clunky you can use AJAX to make it all happen inline. I'm sure there's other ways too, but it depends how you want the interface to behave.

Railscasts - Free Ruby on Rails Screencasts

Re: RESTful bulk has_and_belongs_to_many

Thy for the answer, but this is too complicated for my users. I'm afraid that they won't use it at all.

My LanguageSkill has two properties:

level    :integer # [1,2,3]
remarks  :text

I'll try to implement it using radio buttons. Not nice, but it should work.