Mike
Mike

Reputation: 89

has_one and mongoid: undefined method `empty?' for #<User>

Getting undefined method `empty?' for # error in my controller when trying to save the currently logged_in user to the model.

Using devise and rails 4.

Model:

  class Event

  include Mongoid::Document

  field :name, type: String
  field :description, type: String
  field :date, type: Date

  embeds_many :invitees, cascade_callbacks: true
  embeds_many :participants, cascade_callbacks: true
  embeds_many :comments, cascade_callbacks: true
  embeds_many :options, cascade_callbacks: true

  has_one :owner, :class_name => "User"

  accepts_nested_attributes_for :options, autosave: true, allow_destroy: true
  accepts_nested_attributes_for :participants, autosave: true, allow_destroy: true
  accepts_nested_attributes_for :comments, autosave: true, allow_destroy: true
  accepts_nested_attributes_for :invitees, autosave: true, allow_destroy: true
  accepts_nested_attributes_for :owner, autosave: true, allow_destroy: true

  end

Controller: ... def create

    @event = Event.create(event_params)

    if user_signed_in?
      @event.create_owner(current_user)
    end

    respond_to do |format|
      if @event.save
        #TODO: Save users attached to event in user collection
        format.html { redirect_to @event, notice: 'Event was successfully created.' }
        format.json { render action: 'show', status: :created, location: @event }
      else
        format.html { render action: 'new' }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end
  ....

What am I doing wrong?

Edit: Here is the stacktrace:

C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/attributes/processing.rb:21:in process_attributes' C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/document.rb:110:inblock in initialize' C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/threaded/lifecycle.rb:84:in _building' C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/document.rb:104:ininitialize' devise (3.0.4) lib/devise/models/confirmable.rb:46:in initialize' C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/factory.rb:23:in new' C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/factory.rb:23:in build' C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/relations/builders.rb:93:in block in creator' app/controllers/events_controller.rb:31:in create' actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in send_action' ...

Upvotes: 0

Views: 1032

Answers (1)

Mike S
Mike S

Reputation: 11409

The problem is that you're calling the function "empty?" on a User record when it is not defined. In the simplest case this fails in the rails console:

1.9.3-p194 :001 > User.first.empty?
    User Load (9.3ms)  SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
    NoMethodError: undefined method `empty?' for #<User:0x007fe0375d8c10>

You could fix this by stubbing out the function in your User model:

def empty?
end

But it would be better to figure out where you're making the call and fix it. The error trace should tell which file and line it originates from.

Upvotes: 0

Related Questions