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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:06:35+00:00 2026-05-20T10:06:35+00:00

I am using Ruby on Rails 3 and I am trying to build an

  • 0

I am using Ruby on Rails 3 and I am trying to build an ActiveModel this way:

module Users # I use a namespace
  class Account
    extend ActiveModel::Naming
    extend ActiveModel::Translation
    include ActiveModel::Validations

    attr_accessor :name
    attr_reader :errors

    def initialize
      @errors = ActiveModel::Errors.new(self)
    end

    def validate!
      errors.add(:name, "can not be nil") if name == nil
    end

    # The following methods are needed to be minimally implemented

    def read_attribute_for_validation(attr)
      send(attr)
    end

    def Account.human_attribute_name(attr, options = {})
      attr
    end

    def Account.lookup_ancestors
      [self]
    end


    def persisted?
      false
    end
end

If in my controller I make this

def create
  @account = Users::Account.new
  @account.errors.add( :name, "can not be blank" )
  ...
end

I get this error:

undefined method `add' for nil:NilClass

If I make

@new_account = Users::Account.new

the debug of @new_account is

--- !ruby/object:Users::Account 
id: 
name: 
surname: 
updated_at: 

Where is the error and how can I solve that?


P.S.: I don’t want to use validate_presence_of because I need a different validation process, but I think that also that method doesn’t work.


If I use the validate! method I get the following error that refers to that

NoMethodError in Users/accountsController#create

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]=

Application trace
pp/models/users/account.rb:16:in `validate!'
  • 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-20T10:06:35+00:00Added an answer on May 20, 2026 at 10:06 am

    I think ActiveModel::Validations automatically defines errors for you. You are probably overwriting this when you define

    attr_reader :errors
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an app using Ruby on Rails and I'm trying to
What is the simplest way to send mail using Ruby on Rails? Is there
Using Snow leopard, Rails 3.0.3, ruby 1.9.2p0 and RVM. When trying to install bundle
I'm trying to build a new rails 3 app from scratch using OmniAuth. Currently
I am using Ruby on Rails. I want to create a filter field on
I am using ruby on rails with a MySQL backend. I have a table
We are developing a considerably big application using Ruby on Rails framework (CRM system)
I installed Ruby and Ruby on Rails yesterday on Vista 32bit using the directions
So I installed Ruby On Rails using the Windows Installer. Now the startup guides
I am using ActiveScaffold in a Ruby on Rails app, and to save space

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.