Question: I have 2 models, Video and ZencoderJob. Each video `has_many` zencoder\_jobs and zencoder\_job `belongs_to` one video.

Now because a video can have multiple jobs (f.e. when you change the video file it will generate a new encoding job), there are many jobs with different encoding states. I use "processing" and "finished" to know when a video is ready to be shown through the API.

This is my actual query from video.rb

```
def self.finished_and_active(limit, offset)
  limit(limit || 30)
  .offset(offset.to_i || 0)
  .order("id DESC")
  .where(:active => true)
  .joins(:zencoder_jobs).where(:zencoder_jobs => { :encoded_state => "finished" })
  .uniq
end

```

The problem is that if there are 3 jobs for one video, 2 old and 1 new, the old jobs have a encoded\_state of "finished" and the new has "processing". It does not look for the last ID of the job but if there is any job, no matter if old or new with "finished", it reveal it to the API.

I tried

```
.joins(:zencoder_jobs).where(:zencoder_jobs => { :encoded_state => "finished" }).last.uniq

```

but it throws an error because of "last". Any idea?

Answer: 
If you can use a real LDAP server I would certainly do that: you will find it a huge step forward. OpenLDAP for example works well and the price is right.

Otherwise there already are embedded LDAP servers out there. The UnboundID SDK that @Terry Gardner often spruiks here contains one of them. I have no affiliation: he may.
[ldapjs](http://ldapjs.org/) can do this, but in Node.js, not Java. [ruby-ldapserver](https://rubygems.org/gems/ruby-ldapserver) is similar, but in Ruby. I am currently searching for an analogue in Java.