title: Rails best practice for creating a model during the creation of a separate model
body: <p>So, I've got two models: <code>User</code> and <code>Group</code>. </p>
  
  <p><code>User belongs_to :group</code></p>
  
  <p>and</p>
  
  <p><code>Group has_many :users</code>. </p>
  
  <p>When creating a new <code>User</code>, I want to check for an already created <code>Group</code> and assign them, or create a new <code>Group</code> based on user-supplied information. <code>Group.find_or_create_by_name("Name")</code> 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 <code>user.rb</code>. 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 <code>user.rb</code> and then call that method in <code>users_controller.rb</code>?</p>
  
  <p>And I guess a related question: I know that my schema should reflect</p>
  
  <pre><code>create_table "users" do |t|
    t.integer "group_id"
  </code></pre>
  
  <p>for the purpose of querying. How exactly does Rails "automagically" link these? When creating my <code>Group</code> model, I'm guessing it needs to have a <code>string:id</code> column, and Rails just links the two?</p>
  
  <p>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.</p>