# Ruby/LDAP on Rails

**URL:** <https://www.ruby-forum.com/t/ruby-ldap-on-rails/54580>\
**Category:** Rails\
**Created:** [March 8, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/ruby-ldap-on-rails/54580 "2006-03-08T23:21:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [March 8, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/ruby-ldap-on-rails/54580/1 "2006-03-08T23:21:04Z")

</div>

This is wierd. I’m trying to get Ruby/LDAP up and working, and at least  
the basics work fine under irb:

irb(main):001:0\> require ‘ldap’  
=\> true  
irb(main):002:0\> LDAP::Conn.new()  
=\> #LDAP::Conn:0x1b87ac  
irb(main):003:0\>

So far, so good. All LDAP::Conn.new does is call ldap\_init with host  
and port parameters, which in turn just allocates the control structure  
– there’s no attempt to create a socket until later.

When I switch to Rails, though, this gets strange. Running inside a  
brand new rails structure (this is 1.0.0), I do:

$ script/console  
Loading development environment.

> > require ‘ldap’  
> > =\> true
> 
> > LDAP::Conn.new()  
> > LDAP::ResultError: can’t initialise an LDAP session  
> > from (irb):2:in `initialize’  
> > from (irb):2
> 
> >

When I first saw this, it was on my mac `ruby -v` -\> ruby 1.8.4  
(2005-12-24) [powerpc-darwin8.5.0] with ruby-ldap-0.9.2, and I’ve  
confirmed the behavior through ruby-ldap-0.9.4.

This isn’t just an OSX thing, though. I see the exact same error on my  
gentoo box (originally 2004.1, but kept current, where `ruby -v` -\> ruby  
1.8.4(2005-12-24) [i686-linux]), with everything up to and including  
ruby-ldap-0.9.2 installed through portage.

Thanks!  
Eric

---

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [March 8, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/ruby-ldap-on-rails/54580/2 "2006-03-08T23:53:46Z")

</div>

On 3/8/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So far, so good. All LDAP::Conn.new does is call ldap\_init with host and  
> port parameters, which in turn just allocates the control structure –  
> there’s no attempt to create a socket until later.

I don’t know anything more about LDAP::Conn than what you wrote here,  
but  
where do the host and port parameters come from? Is it possible that  
they  
aren’t accessible from within Rails?

---

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [March 8, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/ruby-ldap-on-rails/54580/3 "2006-03-08T23:44:35Z")

</div>

On 3/8/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> true
> 
> > > LDAP::Conn.new()  
> > > LDAP::ResultError: can’t initialise an LDAP session  
> > > from (irb):2:in `initialize’  
> > > from (irb):2
> 
> When I first saw this, it was on my mac `ruby -v` → ruby 1.8.4  
> (2005-12-24) [powerpc-darwin8.5.0] with ruby-ldap-0.9.2, and I’ve  
> confirmed the behavior through ruby-ldap-0.9.4.

Sorry, couldn’t duplicate it at work (RHEL):

```
ruby 1.8.2 (2004-12-25) [i386-linux]
ruby-ldap-0.9.2
rails 1.0.0
Red Hat Enterprise 3

$ ./script/console
Loading development environment.
>> require 'ldap'
=> true
>> LDAP::Conn.new()
=> #<LDAP::Conn:0xb6f9f344>

```

If I find the time, I’ll try at home (Mac OS X).

---

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [March 10, 2006, 12:15am UTC](https://www.ruby-forum.com/t/ruby-ldap-on-rails/54580/4 "2006-03-10T00:15:54Z")

</div>

Bah. Found a work around at least.

It looks like this may be an issue with openldap 2.2.19 as installed in  
OSX 10.4.5. I build openldap 2.3.20 (without slapd, to save time),  
installed it under /usr/local, and pointed ruby-ldap at that for the  
build. It works like a champ now.

Thanks to everybody who tried to help.

-Eric

---

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [March 9, 2006, 12:19am UTC](https://www.ruby-forum.com/t/ruby-ldap-on-rails/54580/5 "2006-03-09T00:19:43Z")

</div>

They’re both optional, and default to “localhost” and LDAP\_PORT (port  
389, defined in ldap.c as `rb_define_const (rb_mLDAP, "LDAP_PORT", INT2NUM(389));`

I’ve tried wrapping the actual ldap\_init call with a couple of  
printf()s, and can verify that it appears to be getting the arguments it  
expects.

Josh on Rails wrote:

> I don’t know anything more about LDAP::Conn than what you wrote here,  
> but  
> where do the host and port parameters come from? Is it possible that  
> they  
> aren’t accessible from within Rails?

---

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [April 10, 2006, 9:29pm UTC](https://www.ruby-forum.com/t/ruby-ldap-on-rails/54580/6 "2006-04-10T21:29:19Z")

</div>

In fact, it appears that I was wrong.

The real problem was a shared lib problem between ruby-oci8 and  
ruby-ldap. If you have ruby-oci8 installed and linked against a  
10g-series instantclient, then be aware that libclntsh.so has its own  
version of ldap\_connect() – and it doesn’t appear to work. Even if you  
aren’t using it, the ActiveRecord initialization sequence will load  
oci8lib.so and its dependencies. If you then have your ‘require “ldap”’  
in environment.rb where it gets loaded later, then calls to  
ldap\_connect() will be handled by the oci version, not the openldap  
version.

The workaround I found was to put ‘require “ldap”’ at the _top_ of  
environment.rb, prior to the require of ‘boot’.

May this save someone a bit of time.
