Thanks for the explanation on this. The only issue with adding
limit(1) is that this will return an array and what I am expecting
(from an ActiveRecord mindset) is a single Page. I think it would be
cool if your could have a criteria like 'first' which would tell a
Criteria to only return a single object instead of an array of
objects. Then I could do:
def self.find_by_slug(slug)
self.where(:slug => slug).first
end
Looking at the commit history of Mongoid it looks there used to be
functionality to add a :type option (:all, :first, :last) to a
Criteria. Is there anyway to get a criteria chain to return a single
document now?
On Jun 16, 9:48 pm, Durran Jordan <
dur...@gmail.com> wrote:
> Criteria#first returns an actual Document or nil, so you cannot chain
> it - so your workaround is correct. (It's not actually a workaround,
> its by design.) My suggestion for getting the first one would be to
> use the limit method instead:
>
> def self.find_by_slug(slug)
> where(:slug => slug).limit(1)
> end
>
> That will give you a chainable criteria back with what you are expecting.
>