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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:21:47+00:00 2026-06-15T00:21:47+00:00

I have a User model and a Company model linked like this: class User

  • 0

I have a User model and a Company model linked like this:

class User < ActiveRecord::Base
  belongs_to :company
  accepts_nested_attributes_for :company
end

class Company < ActiveRecord::Base
   has_many :users  
 end

On the sign in page, I want the user to set up both his info (mail, password) and his company info (several fields). So my form looks like this:

<%= simple_form_for @user, :html => { :class => 'form-horizontal' } do |f| %>

        <%= f.input :email, :required => true, :placeholder => "user@domain.com" %>
        <%= f.input :password, :required => true %>
        <%= f.input :password_confirmation, :required => true %>

<h2>Company info</h2>
<%= simple_fields_for :company, :html => { :class => 'form-horizontal' } do |fa| %>
    <%= fa.input :name %>
    <%= fa.input :url %>
    <%= fa.input :description, :as => :text, :input_html => { :cols => 60, :rows => 3  } %>
    <%= fa.input :logo %>
    <%= fa.input :industry %>
    <%= fa.input :headquarters %>
<% end %>

        <div class="form-actions">
            <%= f.submit nil, :class => 'btn btn-primary' %>
            <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
            root_url, :class => 'btn' %>
        </div>

<% end %>

My user model has a company_id:integer field. So logically, when I sign in the user, the first thing to do is to create the Company before the User and then give to the user creation model the appropriate company_id. So I wrote this:

class UsersController < ApplicationController
  before_create :create_company

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      redirect_to root_url, :notice => "Registration successful."
    else
      render :action => 'new'
    end
  end

private

  def create_company
    @company = Company.new(params[:company])
    if @company.save
      self.company_id = @company.id
    else
      render :action => 'new'
    end
  end

end

Problem is: when accessing /users/new I get this error:

undefined method `before_create' for UsersController:Class

What’s going wrong? I checked, before_create has not been deprecated, I’m in Rails 3.2.8. This is probably something stupid with my create_company method but I can’t figure why…

Thanks a lot for helping!

  • 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-06-15T00:21:48+00:00Added an answer on June 15, 2026 at 12:21 am

    before_create is a hook method belonging to ActiveRecord

    before_filter is a hook method belonging to Controller.

    so I suggest you to re-build your code after you make clear which is which. ^_^

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

Sidebar

Related Questions

I have this code: class Company < ActiveRecord::Base has_many :users, :through => :company_users do
I have 3 model class User < ActiveRecord::Base has_many :profiles has_many :companies, :through =>
I have the following User model on my application (simplified) class User < ActiveRecord::Base
I have the following models: class Application(models.Model): users = models.ManyToManyField(User, through='Permission') folder = models.ForeignKey(Folder)
In a Django project as models I have something like that: class Company(models.Model): name
I have a Company Model and a User Model. A User belongs_to a Company
I have created a Company model and a profile model. Every User belongs to
I have a User model, a Post model, and an Interest model. User has_many
I'm newcomer in django, and here is question: I have model class: def Client(models.User)
I have a User and Company model. There are three possible roles that a

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.