1

I have a "posts" model, a "teams" model, and a "post_memberships" model. I want "posts" to be related to many "teams" and "teams" to be related to many "posts". I have everything set up properly (i think), but I am unsure of how to create a "post" record with multiple teams related to it.

    class Post < ApplicationRecord

        has_many :post_memberships
        has_many :teams,through: :post_memberships
    end

    class Team < ApplicationRecord

    has_many :post_memberships
    has_many :posts,through: :post_memberships

end

    class PostMembership < ApplicationRecord

    belongs_to :team
    belongs_to :post

end

My "post" form sends a multiple select field of team_id's to the create action in the posts_controller:

    def create
        @post = Post.new(post_params)
        if post_params[:teams]
          post_params[:teams].each do |id|

            @post.teams << Team.find(id)

          end
        end

        respond_to do |format|
          if @post.save
            format.html { redirect_to @post, notice: 'Post was successfully created.' }
            format.json { render :show, status: :created, location: @post }
          else
            format.html { render :new }
            format.json { render json: @post.errors, status: :unprocessable_entity }
          end
        end
      end

def post_params
      params.require(:post).permit(:title, :body, :teams)
    end

I cannot seem to create a "Post" with a "PostMembership".

AthleteInAction
  • 435
  • 4
  • 14
  • Hi, could you add the ```post_params``` method and the request (console output), so you can make sure that the teams parameter is sent correctly? Because your models and the controller seems to be right. – Ninigi Dec 27 '16 at 07:42
  • def post_params params.require(:post).permit(:title, :body, :teams) end – AthleteInAction Dec 27 '16 at 07:45
  • Does it give you an error, or does it return just fine but no post created? – Ninigi Dec 27 '16 at 07:48
  • It creates the post perfectly, just no "PostMembership" – AthleteInAction Dec 27 '16 at 07:48
  • Right now I am thinking I can only create a PostMembership after the post has been created, unless I can find a way to create them both simultaneously. – AthleteInAction Dec 27 '16 at 07:50
  • Ok, I think we need the params here, could you put ```p params``` in ```def create``` and post the console output (or whatever debug method you prefer) :) – Ninigi Dec 27 '16 at 07:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131549/discussion-between-ninigi-and-athleteinaction). – Ninigi Dec 27 '16 at 07:52

1 Answers1

1

There is a caveat in using arrays in strong parameters, so you need to change your post_params method:

def post_params
  params.require(:post).permit(:title, :body, teams: [])
end

That's not quite the end of it though, because now your Post.new receives an array of ids for the teams association, which should throw AssociationTypeMismatch. So we need to change your create method a tiny little bit:

def create
  @post = Post.new(post_params.except(:teams))
  # ...

everything else looks like it should be working :)

Community
  • 1
  • 1
Ninigi
  • 1,311
  • 12
  • 19