ddonche
ddonche

Reputation: 1065

User_id is always nil upon Group create

I have a problem with my Groups controller. I can create new groups, but the user_id is always nil. I think it has to do with the way I set the association, see here how I have the :creator part

Group.rb

  belongs_to :creator, class_name: "User"
  has_many :memberships
  has_many :users, through: :memberships

User.rb

  has_many :created_groups, class_name: "Group"
  has_many :memberships
  has_many :groups, through: :memberships

groups_controller.rb

  def new
    @group = current_user.groups.build
  end

  def create
    @group = current_user.groups.build(group_params)

    respond_to do |format|
      if @group.save
        format.html { redirect_to @group, notice: 'You have a new Group!' }
      else
        format.html { render :new }
      end
    end
  end

How do I set this up so the current user id is being passed with my current setup?

Upvotes: 0

Views: 45

Answers (1)

Mahmoud Sayed
Mahmoud Sayed

Reputation: 668

It should be

@group = current_user.created_groups.build(group_params)

Upvotes: 1

Related Questions