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

  • Home
  • SEARCH
  • 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 6909165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:39:18+00:00 2026-05-27T08:39:18+00:00

So, I’ve got two models: User and Group . User belongs_to :group and Group

  • 0

So, I’ve got two models: User and Group.

User belongs_to :group

and

Group has_many :users.

When creating a new User, I want to check for an already created Group and assign them, or create a new Group based on user-supplied information. Group.find_or_create_by_name("Name") seems like a great way to do this, but my question is where should this be located? I’m guessing that following “Fat Models, skinny Controllers” it should be in user.rb. If it’s in the model, what is the best way to pass in user-supplied strings on user creation? Should I set up a method in user.rb and then call that method in users_controller.rb?

And I guess a related question: I know that my schema should reflect

create_table "users" do |t|
  t.integer "group_id"

for the purpose of querying. How exactly does Rails “automagically” link these? When creating my Group model, I’m guessing it needs to have a string:id column, and Rails just links the two?

Sorry for my obvious lack of understanding on associations, but I’m trying not to just hack something together that works awkwardly, and want to make sure I understand the best way to go about it.

  • 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-27T08:39:20+00:00Added an answer on May 27, 2026 at 8:39 am

    First of all, according to what you wrote, a group can have multiple users in it, but a user can only be part of a single group.

    Usually, a group can have multiple members and a user can be member of multiple groups. To achieve that, you create an association model, called membership.

    So, your user model have :

    has_many :memberships
    has_many :groups, :through => :memberships
    

    And your group model have :

    has_many :memberships
    has_many :members, :through => memberships
    

    The association will have a table on it’s own that will be something like :

    create_table "memberships" do |t|
        t.integer "group_id"
        t.integer "user_id"
        t.timestamps
    end
    
    add_index :memberships, :group_id
    add_index :memberships, :user_id
    

    To answer your original question, if you have to create a group when a user is created regardless of the way he is created, I would create an Active Record Callback in the user model :

    after_create :create_group
    
    def create_group
       # whatever logic and create group
    end
    

    For more information about Active Record Callbacks, see http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

    If the group you are creating is related to the information entered in a form (view) and is not directly related to the user, then you would have to do the creation of the group in the controller. Something like :

    def create
       @group = Group.build(params[:group])
       @group.save!
       @user = User.build(params[:user])
       @user.group_id = @group.id
       @user.save!
    end
    

    Or, cleaner way :

    def create
       @group = Group.build(params[:group])
       @group.save!
       @user = @group.users.build(params[:user])
       @user.save!
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
i got an object with contents of html markup in it, for example: string

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.