3

I am trying to initialize a new user within the rails console of my app. Whenever I enter the initialization code:

irb(main):001:0> user = User.new

I get this error:

(Object doesn't support #inspect)
=>
irb(main):002:0>

I have the user credentials set up properly, and there isn't any use of code the wrong way.

class User
include Mongoid::Document

# User fields
field :first_name, type: String
field :last_name, type: String
field :phone_number, type: Integer
field :address, type: String
field :info, type: String
field :portfolio, type: String
field :motto, type: String
field :cover, type: BSON::Binary
field :avatar, type: BSON::Binary
field :photo, type: BSON::Binary
field :request, type: Boolean
field :decision, type: Boolean
field :gender, type: String
field :user_url, type: Mongoid::EncryptedHash
field :position, type: String
field :created_at, type: DateTime, default: ->{DateTime.now}
field :updated_at, type: DateTime
field :view_comments_count, type: Integer
field :thumbs_up_notifications, type: Boolean
field :tracking_notifications, type: Boolean
field :message_notifications, type: Boolean
field :applaud_notifications, type: Boolean
field :shout_out_notifications, type: Boolean
field :congrats_notifications, type: Boolean
field :post_notifications, type: Boolean


# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

## Database authenticatable
field :email,              type: String, default: ""
field :password, type: Mongoid::EncryptedString, default: ""

## Recoverable
field :reset_password_token,   type: Mongoid::EncryptedString
field :reset_password_sent_at, type: Time

## Rememberable
field :remember_created_at, type: Time

## Trackable
field :sign_in_count,      type: Integer, default: 0
field :current_sign_in_at, type: Time
field :last_sign_in_at,    type: Time
field :current_sign_in_ip, type: String
field :last_sign_in_ip,    type: String

## Confirmable
field :confirmation_token,   type: String
field :confirmed_at,         type: Time
field :confirmation_sent_at, type: Time
field :unconfirmed_email,    type: String # Only if using reconfirmable

## Lockable
field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is      :failed_attempts
field :unlock_token,    type: String # Only if unlock strategy is :email or :both
field :locked_at,       type: Time

attr_accessor :verify_password, :password_confirmation

attr_reader :comment_notifications, :post_notifications,
          :shout_out_notifications, :thumbs_up_notifications,
          :applaud_notifications, :tracking_notifications,
          :congrats_notifications, :message_notifications,
          :request_notifications, :acceptance_notifications


validates :first_name, presence: true
validates :last_name, presence: true
validates :email, presence: true
validates :password, presence: true
validates :phone_number, presence: true
validates :address, presence: true
validates :info, presence: true
validates :motto, presence: true
validates :gender, presence: true

validates_confirmation_of :password

has_many :posts
has_many :comments
has_many :thumbs_ups
has_many :congrats
has_many :tracks
has_many :shout_outs
has_many :applauds
has_many :assignments
has_one :position
has_one :rank
belongs_to :group


after_create :find_default_contacts, :create_url

before_save :validates_email, :validates_motto, :validates_info

If someone could please point out what is causing the error, I would appreciate it.

metaco57
  • 155
  • 3
  • 15
  • can you show me your migration file for users.. just copy and paste from schema? – Aniket Rao Jan 20 '17 at 02:24
  • I'm using a NoSQL database: MongoDB. There are no migrations. – metaco57 Jan 20 '17 at 04:14
  • could you paste the full user.rb code and your gemfile please? – Aniket Rao Jan 20 '17 at 06:25
  • Could you please paste your `User` model I think you are overriding `initialize` method If yes then you should call super on your `initialize` method – Rahul Jan 20 '17 at 07:28
  • There actually isn't an initialize method. Could this be the reason that the error keeps displaying? – metaco57 Jan 20 '17 at 14:52
  • Also, the User model is over 400 lines of code long. – metaco57 Jan 20 '17 at 15:46
  • I have the same issue, when I comment out `devise` helpers it goes away: `devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :confirmable, :invitable, invite_for: 1.day` – Ben Orozco Apr 03 '19 at 20:12

2 Answers2

1

The instantiation of your User object and its assignment to the user variable seem to be working just fine, so I would expect that you actually do have a new instance of User stored in user. It would appear though that some code that you are including in your User class is undefining User's #inspect method.

All classes in Ruby, including your User class, inherit from the Object class, and Object defines an instance method called inspect. #inspect usually calls #to_s to return a string representation of the object. IRB uses this method to produce the output that you'd normally see if the method was defined.

You should be able to see how this works be defining #inspect yourself:

class User
  def inspect
    "custom implementation of #inspect"
  end
end


irb(main):001:0> user = User.new
=> custom implementation of #inspect
irb(main):002:0>

So you can provide your own #inspect if you like, but it's odd that you would need to. If you want to track this down, I would look through the code that you're including in User (apparently just Mongoid::Document) and try to find where and why #inspect is being undefined.

edurante
  • 71
  • 1
  • 3
  • I don't see any methods that would make #inspect undefined? – metaco57 Jan 21 '17 at 16:16
  • Just found that several methods in the file are causing the error. But I don't know which ones. Is there a way to identify them. – metaco57 Jan 21 '17 at 16:40
  • I'm having the same situation but I have this code in my model `class User < ApplicationRecord enum role: [:seller, :buyer, :admin] devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable after_initialize :set_default_role, :if => :new_record? def set_default_role self.role ||= 'seller' end def name (self.username || self.email.split('@').first).humanize end` When I comment out devise things it works just fine maybe some module in devise hooks missing the #inspect method – Muhammad Nasir Shamshad May 05 '19 at 01:59
  • @edurante I defined the inspect method and it just prints out the custom text but didn't yields the proper results that I am expecting from `User.all` or any other query on model. – Muhammad Nasir Shamshad May 05 '19 at 02:07
0

I figured out that I was using custom devise path_names in routes.rb like this

devise_for :users, path_names: { sign_in: 'login', sign_out: 'logout' }

when I removed the path_names that error goes away.

Hope this help someone someday.