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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:49:29+00:00 2026-05-17T01:49:29+00:00

Hi (huge Rails newbie here), I have the following models: class Shop < ActiveRecord::Base

  • 0

Hi (huge Rails newbie here), I have the following models:

class Shop < ActiveRecord::Base
  belongs_to :user
  validates_uniqueness_of :title, :user_id, :message => "is already being used"
end

and

class User < ActiveRecord::Base
  has_one :shop, :dependent => :destroy
end

When I’m about to create a new shop, I get the following error:

private method `create' called for nil:NilClass

This is my controller:

@user = current_user
@shop = @user.shop.create(params[:shop])

I’ve tried different variations by reading guides and tutorials here and there, but I’m more confused than before and can’t get it to work. Any help would be greatly appreciated.

  • 1 1 Answer
  • 2 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-17T01:49:30+00:00Added an answer on May 17, 2026 at 1:49 am

    First of all, here is how to do what you want:

    @user = current_user
    @shop = Shop.create(params[:shop])
    @user.shop = @shop
    

    Now here’s why your version did not work:

    You probably thought that this might work because if User had a has_many relation to Shop, @user.shops.create(params[:shop]) would work. However there is a big difference between has_many relations and has_one relations:

    With a has_many relation, shops returns an ActiveRecord collection object, which has methods that you can use to add and remove shops to/from a user. One of those methods is create, which creates a new shop and adds it to the user.

    With a has_one relation, you don’t get back such a collection object, but simply the Shop object that belongs to the user – or nil if the user doesn’t have a shop yet. Since neither Shop objects nor nil have a create method, you can’t use create this way with has_one relations.

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

Sidebar

Related Questions

The following is beginning to become a huge problem for us. We have about
I have the following scenario: I have a rails web portal (Rails 3.1) and
I've got a User model in my Rails 3 app which is getting huge.
I have huge Rails app on development right now, which run VERY slow on
Newbie to Rails here, I was wondering if there is a tool which can
I have a Movie class in my Rails app, and I have a _movie.html.erb
I have a huge XML document 200MB in size containing textual information. The data
I have a huge checkbox list with different levels and I would like to
I have a huge java codebase (more than 10,000 java classes) that makes extensive
I have a huge pl/sql stored procedure, where we make some deletions as long

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.