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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:21:12+00:00 2026-05-27T20:21:12+00:00

I’m new to ruby on rails, and I’ve inherited a codebase. Right now I

  • 0

I’m new to ruby on rails, and I’ve inherited a codebase. Right now I can’t create a new user account in my application, when I try I get the following error:

NoMethodError in Users::RegistrationsController#create

undefined method `name’ for nil:NilClass
Rails.root: /home/nathan/dev/legwork-core

Application Trace | Framework Trace | Full Trace
app/models/contact.rb:74:in block (2 levels) in <class:Contact>'
app/models/user.rb:31:in
update_contact’

The calling code is here:

  searchable do
    string :category do
      self.category.name
    end
  end

Category is supposed to be an instance of ContactCategory, and what I think I need is to set self.category to the default if its nil. I tried this to fix it:

  after_initialize :set_defaults

  def set_defaults
    self.category = ContactCategory.first if self.category.nil?
  end

I also tried:

  def after_initialize
    self.category = ContactCategory.first if self.category.nil?
  end

And I’ve tried:

  before_create :set_defaults

  def set_defaults
    self.category = ContactCategory.first if self.category.nil?
  end

Someone else has suggested putting this logic in before_save, but there is already a before_save that has this logic in it, that’s where I saw what the author had intended to be the default in the first place.

UPDATE:

This question is silly now that I see what’s wrong. I was assuming that the assignment statement never ran because I assumed ContactCategory.first was also not nil. Sadly, everything here is working as expected. The moral of the story is:

All of the hooks I was using to set the default were working correctly. I would recommend using them to set a default using ActiveRecord.

  • 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-27T20:21:13+00:00Added an answer on May 27, 2026 at 8:21 pm

    I tend to use the before_create callback for setting default values and whatnot, but here’s the full list of ActiveRecord callbacks from the RoR API docs. You’ll notice after_initialize doesn’t work like exactly its name suggests:

    Lastly an after_find and after_initialize callback is triggered for each object that is found and instantiated by a finder, with after_initialize being triggered after new objects are instantiated as well.

    You probably want to use before_save or before_create instead.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the 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.