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 7009927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:55:30+00:00 2026-05-27T21:55:30+00:00

Specifically, I have a form for creating a User . One of the fields

  • 0

Specifically, I have a form for creating a User. One of the fields is Group, which is a separate model. During the User#create action I call Group.find_or_create_by_name to check and see if the Group already exists by pulling out params[:user][:group], and create the Group if it doesn’t exist.

But, when I create the User, I can’t pass params[:user], because params[:user][:group] is not a group, it’s a String. This would be a lot easier if I could supply params[:user] and params[:group] to my controller, instead of everything bundled under a single variable, but I don’t know how to do that.

Relevant code:

User#create

@group = Group.find_or_create_by_name(params[:user][:group])
@group.save!
@user = @group.users.build(params[:user])

Partial User Schema

create_table "users", :force => true do |t|
  t.string   "name",                :default => "",    :null => false
  t.string   "email",                                  :null => false
  t.integer  "group_id"

Partial Group Schema

create_table "groups", :force => true do |t|
  t.string   "name"
  t.text     "description"
  t.datetime "created_at"
  t.datetime "updated_at"

Params Dump from form submission

{"commit"=>"Register",
 "authenticity_token"=>"x1KgPdpJop5H2NldsPtk0+mBDtrmpM/oNABOxjpabIU=",
  "utf8"=>"✓",
  "user"=>{"name"=>"aName",
  "group"=>"aGroupName",
  "password_confirmation"=>"[FILTERED]",
  "password"=>"[FILTERED]",
  "email"=>"anEmail"}}

The best explanation I’ve run across is this question.

I don’t really want to start rearranging the params has inside of my controller as that really makes me queasy, and seems like terrible programming. Which really makes me question whether I’ve got a bigger problem.

If it goes against Rails conventions to do this, what then is the best way to do it, and why should I not?

  • 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-27T21:55:31+00:00Added an answer on May 27, 2026 at 9:55 pm

    So, I’m going to spell out everything I’ve learned, in case other people are having trouble understanding this like I did. Also, if I get anything wrong here, please correct me.

    If you’re using a form that creates multiple model instances at once (preferably associated ones), you first need to use the helper accepts_nested_attributes_for in your model definition (probably right underneath your declared associations). The reason for this is it creates a setter method that knows how to write that type of associated model. (Note: you can also define this method yourself in your main model). Once you’ve done that you can nest a fields_for inside of a form_for, and Rails will know how to make the proper assignments.

    I initially thought that accepts_nested_attributes_for was referring to nested resources, which is definitely not the case. If you’re looking for more information, refer to section 11.8.3 (pp 343-347) of The Rails 3 Way.

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

Sidebar

Related Questions

I have a contact form in my flash file with name/email/message fields which a
I have a form with series of 3 file upload fields, each of which
I have a custom list in SharePoint (specifically, MOSS 2007.) One field is a
Specifically, I have a model that has a field like this pub_date = models.DateField(date
i have a form containing inputs for times (specifically, an opening and closing time).
I have a form which lets users input text snippets. So how can figure
I have a form that is using an anchor name as it's action so
I have a form that is generated based on several DataTemplate elements. One of
I have a base User form that I am subclassing for each use case,
I am trying to make a form with some dynamic behavior. Specifically, I have

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.