Skip to content

Instantly share code, notes, and snippets.

@martinklepsch
Created March 14, 2012 21:16
Show Gist options
  • Save martinklepsch/2039587 to your computer and use it in GitHub Desktop.
Save martinklepsch/2039587 to your computer and use it in GitHub Desktop.

[1] pry(#)> self.member = Array.new

=> []

[2] pry(#)> self.member.push( User.find(memberUid.first).dn )

NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.push

The code in the ruby file is a little different. The above resulted by removing the each loop for debugging.

[1] pry(#<Group>)> puts "#{self.member.class}" 
Array
=> nil
[2] pry(#<Group>)> self.member = []
=> []
[3] pry(#<Group>)> puts "#{self.member.class}"
NilClass
=> nil
class Group < ActiveLdap::Base
ldap_mapping :dn_attribute => "cn",
:prefix => "ou=Groups",
:classes => ["top",
"posixGroup",
"groupOfNames"
]
has_many :users,
:wrap => "memberUid",
:class_name => "User",
:primary_key => 'uid'
before_save :set_members_array
def set_members_array
# TODO MKL fix this
require 'pry'
binding.pry
self.member = []
memberUid.each{ |uid|
self.member.push( User.find(uid).dn )
}
end
def as_json(options = {})
self.attributes
end
end

The problem here was, that when setting an ActiveLdap attribute to an empty array (Array.new or just []) it will set the actual attribute to nil. You can avoid this by never setting an attribute to be an empty array. Means using an extra variable or something like this

self.member = memberUid.collect{|uid| User.find(uid).dn}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment