Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleObject.last not implemented #8

Open
d5e opened this issue Nov 25, 2010 · 3 comments
Open

SimpleObject.last not implemented #8

d5e opened this issue Nov 25, 2010 · 3 comments

Comments

@d5e
Copy link

d5e commented Nov 25, 2010

I have found that in simply_stored/lib/simply_stored/couch/finders.rb there is a :first method implemented, so I can fetch the first record of that type. Working.

But I am missing a :last method allowing me, to get the last record ...

should be something like

def last(*args)
  find(:last, *args)
end

as the first query is made via

CouchPotato.database.view(all_documents(:limit => 1)).first

it seems that a performant approach has to look different for last as for first as we cannot use that :limit => 1 here

@roidrage
Copy link
Contributor

Yrah, it would have to look different, but that's not hard to achieve with CouchDB, as you can tell it to walk the view's b-tree backwards. I'll look into it.

@d5e
Copy link
Author

d5e commented Dec 28, 2010

hey matt, I just found out one way of achieving this MyModel.last thing ... I am not sure whether there is a more generic method, but If you specify a view for the model like

view :latest_view, :key => :created_at, :descending => true, :limit => 1

def self.last
    CouchPotato.database.view(Document.latest_view).first
end

this would return the last Document

@d5e
Copy link
Author

d5e commented Dec 28, 2010

I just found out one more option ... without the need to define a view inside the model

#simply_stored/couch/finders.rb
        when :last
          if with_deleted || !soft_deleting_enabled?
            CouchPotato.database.view(all_documents(:limit => 1, :descending => true)).first
          else
            CouchPotato.database.view(all_documents_without_deleted(
              :key => nil, :limit => 1, :descending => true)).first
          end
        else

best regards!

Henry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants