Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 978691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:05:48+00:00 2026-05-16T04:05:48+00:00

I have a User and Group model. User has_many Groups and Group belongs_to User

  • 0

I have a User and Group model. User has_many Groups and Group belongs_to User

I already have entries in the user table:

id    name
--------------
1     testuser
2     someotheruser

Imagine if the user with id 1 (testuser) is logged in and I want to create groups inside that user.

When I create a new group from new action in Group Controller the entries in the DB are going like this:

   id   groupname   user_id
   ------------------------
   1    groupA
   2    groupB

as you see, user_id, column is always going in as blank so the association is not there.

What do I need to do to make this association?

code for GroupsController:

  def new
    @group = Group.new
  end

  def create
    @group = Group.new(params[:group])
    if @group.save
      flash[:notice] = "Successfully created group."
      redirect_to groups_url
    else
      render :action => 'new'
    end
  end

code for views/groups/new.html.erb

<% title "New Group" %>

<% form_for @group do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </p>
  <p><%= f.submit %></p>
<% end %>


<p><%= link_to "Back to List", groups_path %></p>

I am new to rails please tell me how to make association with the user. From the console I am able to make the association by adding groups for a user like this:

@u = User.find(1)
@u.groups.create(:groupname=>"groupA")
@u.save

but I dont know how to get this done from the front end.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:05:48+00:00Added an answer on May 16, 2026 at 4:05 am

    First you’ll want to make sure that a group can’t be saved without a user:

    /app/models/group.rb

    class Group
      belongs_to :user
      validates_presence_of :user
    end
    

    Then, lets default the group to be the current user.

    /app/controllers/groups_controller.rb

    def create
      @group = Group.new(params[:group])
      @group.user = current.user
      if @group.save
        ...
      else
        ...
      end
    end
    

    You could also do a @group.user = current_user if @group.user.nil? but it sounds like the only person to create a group will be the current user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two models, Group and User User belongs_to Group and Group has_many
I have two models, user and group. I also have a joining table groups_users.
So I have 2 models. Users and Groups. Each group has a user as
I have a User model that has_many :posts. If I wanted to make a
I have the models User and Group, and the join table Membership which uses
I have a select_tag like this: <%= select_tag User Groups, options_from_collection_for_select(@user_groups, id, name) %>
I have a model Group and model User They are connected both directions with
I have a simple model class User has_many :logs class Logs related in the
I have 2 models: class User < ActiveRecord::Base has_and_belongs_to_many :groups end class Group <
I have 4 models, Message, Group, User, Membership class Group < ActiveRecord::Base has_many :memberships

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.