Rails routing for has_and_belongs_to_many relationship

seanoshea

I'm working on a rails 4 API for a sports team where I have players and teams and I'm struggling a little with rails routing and a has_many relationship. My relationship between players and teams looks like:

class Team < ActiveRecord::Base
  extend Searchable
  validates :title, presence: true
  has_and_belongs_to_many :players
end

class Player < ActiveRecord::Base
  extend Searchable
  validates :first_name, presence: true
  validates :last_name, presence: true
  has_and_belongs_to_many :teams
end

I'd like to be able to add an existing player to a team, but I'm unsure of how to change my routes.rb file. Currently, it looks like:

Rails.application.routes.draw do
  devise_for :users
  namespace :api, defaults: { format: :json },
            constraints: { subdomain: 'api' }, path: '/'  do
    scope module: :v1 do
      resources :users, :only               => [:show, :create, :update, :destroy]
      resources :teams, :only               => [:show, :create, :update, :destroy, :index]
      resources :players, :only             => [:show, :create, :update, :destroy, :index]
      resources :sessions, :only            => [:create, :destroy]
    end
  end
end

which allows for CRUD operations on players and teams models. I was thinking that for adding a player to an existing team, my route would need to look like:

/teams/:team_id/add_player/

but I'm unsure of how to declare that route in routes.rb. So, a couple of questions:

  1. Does that route make sense to people from a REST-ful perspective? If so, how would I declare this route in routes.rb
  2. Should it be a PATCH or a POST method for adding a player to a team?

Thanks for any help offered,

Sean

Yury Lebedev

You can declare this route like this:

resources :teams, only: [:show, :create, :update, :destroy, :index] do
  put 'add_player', on: :member
end

It will map the route to the add_player action in your TeamsController.

From the REST-ful perspective i would suggest you to make this in the players#update action though, since you are basically changing the player record.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails routing for has_and_belongs_to_many relationship

From Dev

Recording data with has_and_belongs_to_many relationship in rails

From Dev

Using uniq in a has_and_belongs_to_many relationship in Rails 4

From Dev

rails: accessing table from has_and_belongs_to_many relationship

From Dev

Attach model has_and_belongs_to_many relationship in rails

From Dev

Recording data with has_and_belongs_to_many relationship in rails

From Dev

How do I set up the rails models in a has_and_belongs_to_many relationship?

From Dev

Rails ActiveRecord : How can I give custom order for has_and_belongs_to_many relationship join table?

From Dev

Rails has_and_belongs_to_many with Many

From Dev

Rails has_and_belongs_to_many query

From Dev

Rails has_and_belongs_to_many ActiveRecord::UnknownPrimaryKey

From Dev

Rails eager load with has_and_belongs_to_many

From Dev

Rails: has_and_belongs_to_many with "master" record

From Dev

RAILS-4 - has_and_belongs_to_many

From Dev

Rails eager load with has_and_belongs_to_many

From Dev

Rails has_and_belongs_to_many query

From Dev

Rails 4 has_and_belongs_to_many

From Dev

Rails Converting a has_many relationship into a has and belongs to many

From Dev

rails association - has_many vs has_and_belongs_to_many

From Dev

ng-repeat, filter and a has_and_belongs_to_many relationship

From Dev

Rails query for models having has_many and belongs_to relationship

From Dev

Rails Model relationship has_many belongs_to

From Dev

Rails belongs_to and has_many foreign_key relationship

From Dev

Rails, using has_and_belongs_to_many with abstract Model

From Dev

rails 4 - using has_and_belongs_to_many association

From Dev

Rails 4 Migration: has_and_belongs_to_many table name

From Dev

How to create join table records in has_and_belongs_to_many rails

From Dev

Rails 4.2 saving has_and_belongs_to_many association Ids

From Dev

Rails 4 - checkboxes for has_and_belongs_to_many association

Related Related

  1. 1

    Rails routing for has_and_belongs_to_many relationship

  2. 2

    Recording data with has_and_belongs_to_many relationship in rails

  3. 3

    Using uniq in a has_and_belongs_to_many relationship in Rails 4

  4. 4

    rails: accessing table from has_and_belongs_to_many relationship

  5. 5

    Attach model has_and_belongs_to_many relationship in rails

  6. 6

    Recording data with has_and_belongs_to_many relationship in rails

  7. 7

    How do I set up the rails models in a has_and_belongs_to_many relationship?

  8. 8

    Rails ActiveRecord : How can I give custom order for has_and_belongs_to_many relationship join table?

  9. 9

    Rails has_and_belongs_to_many with Many

  10. 10

    Rails has_and_belongs_to_many query

  11. 11

    Rails has_and_belongs_to_many ActiveRecord::UnknownPrimaryKey

  12. 12

    Rails eager load with has_and_belongs_to_many

  13. 13

    Rails: has_and_belongs_to_many with "master" record

  14. 14

    RAILS-4 - has_and_belongs_to_many

  15. 15

    Rails eager load with has_and_belongs_to_many

  16. 16

    Rails has_and_belongs_to_many query

  17. 17

    Rails 4 has_and_belongs_to_many

  18. 18

    Rails Converting a has_many relationship into a has and belongs to many

  19. 19

    rails association - has_many vs has_and_belongs_to_many

  20. 20

    ng-repeat, filter and a has_and_belongs_to_many relationship

  21. 21

    Rails query for models having has_many and belongs_to relationship

  22. 22

    Rails Model relationship has_many belongs_to

  23. 23

    Rails belongs_to and has_many foreign_key relationship

  24. 24

    Rails, using has_and_belongs_to_many with abstract Model

  25. 25

    rails 4 - using has_and_belongs_to_many association

  26. 26

    Rails 4 Migration: has_and_belongs_to_many table name

  27. 27

    How to create join table records in has_and_belongs_to_many rails

  28. 28

    Rails 4.2 saving has_and_belongs_to_many association Ids

  29. 29

    Rails 4 - checkboxes for has_and_belongs_to_many association

HotTag

Archive