Alexandr Kurilin
Alexandr Kurilin

Reputation: 7845

Many-to-many associations and REST?

Newbie question, you've been warned!

I'm trying to implement a sample Rails app with a many-to-many association, people owning movies, and I'm trying to figure out how exactly to implement the UI for it. It's my understanding that REST requires everything to be a resource, so in this case "User" (person), "Movie" and "Possession" (the joint table) (oh, the puns).

Now the interesting part, the UX. Let's say I have a user dashboard where all of your movies are listed.

Let's say the user wants to add a movie that he owns. How do you do this in REST? It's trivial with a custom action that one could add to the User controller, but the point is not to go beyond the basic 7 REST actions, right? Therefore I'd have to first do a "new" on a movie and then do a "new" on a possession, which are two operations. How do I collapse them into one?

Basically I feel I'm not quite understanding how to maintain REST as soon as multiple models are involved and would appreciate a tip.

Thanks!

Upvotes: 1

Views: 424

Answers (1)

Jordan Running
Jordan Running

Reputation: 106027

Happily, Rails has some magic just for this common scenario. Assuming a model like this:

class Movie
  has_many :users, :through => :possessions
end

Your view:

<%= form_for [current_user, Movie.new] do |f| %>
  <%= f.label       :title %>
  <%= f.text_field  :title %>
<% end %>

Basically this form will POST to MoviesController#create and will pass along current_user.id as a user_id parameter that (the default) MoviesController#create will know to associate with the Movie it creates. Take a look at the documentation for FormBuilder#form_for for more information.

You could also do this the other way around, by the way:

class User
  has_many :movies, :through => :possessions

  accepts_nested_attributes_for :movies # magic!
end

And the view:

<%= form_for current_user |user_form| %>
  <%= user_form.fields_for current_user.movies.build |movie_fields| %>
    <%= movie_fields.label      :title %>
    <%= movie_fields.text_field :title %>
  <% end %>
<% end %>

In this case the form will submit to UsersController#update and its parameters will look like this:

{ :id => 123,
  :movie => {
    :title => "The Red Balloon"
  }
}

...and the controller will know to create the Movie object. For more information check the documentation for FormHelper#fields_for.

Upvotes: 1

Related Questions