SupremeA
SupremeA

Reputation: 1621

"NoMethodError: undefined method `object' for #<Tenant:blahblah>" Error

Hello I am having an error of "NoMethodError: undefined method `information_requisition' for #" I cant seem to relate this model to my user. My code for information_requisition model is:

class InformationRequisition
  include Mongoid::Document
    belongs_to :user
    belongs_to :admin
    has_many :reports

And my user model is:

class User
  include Mongoid::Document
  mount_uploader :avatar, AvatarUploader
  has_one :report,
  has_many :information_requisitions, dependent: :destroy
  has_many :admin
  has_one :bill, dependent: :destroy

And I keep getting the error when I try and display a create form in a partial on a dashboard. Here is where the error shows on my dashboard controller:

class Tenants::DashboardsController < ApplicationController before_filter :authenticate_tenant!

  def show
    @dashboard = current_user
    @bill = current_user.bill || current_user.build_bill
    @information_requisition = current_user.information_requisition || current_user.build_information_requisition
  end

My information_requisition seems to not relate to the user. It keeps giving me the "NoMethodError: undefined method `information_requisition' for #" error. Any idea what I have wrong?

Upvotes: 0

Views: 957

Answers (1)

wintermeyer
wintermeyer

Reputation: 8318

A current_user only has access to information_requisitions (plural) because it is a has_many association.

If @information_requisition is singular you have add a .first after the plural to get the first one. But my guess is that you really want to use first_or_initialize:

def show
    @dashboard = current_user
    @bill = current_user.bill || current_user.build_bill
    @information_requisition = current_user.information_requisitions.first_or_initialize
end

Upvotes: 1

Related Questions