Mongoid pagination - pagination

I tried
#posts = Post.page(params[:page]).per_page(10)
and
#posts = Post.paginate(:page => 1, :per_page => 10)
but neither method works
undefined method `page' for Post:Class
undefined method `paginate' for Post:Class
How do you do pagination with mongoid?

You should use Kaminari https://github.com/amatsuda/kaminari

This works fine for me:
#posts = Post.paginate(:page => 1, :limit => 10).desc(:_id)
desc(:_id) is added so that latest posts could be listed first.

Still using will_paginate is also okay.
This thread has same issue: undefined method `paginate' for Array on Rails 3 with mongoid
The main point to fix the error is to add this line before the controller call paginate library:
require 'will_paginate/array'
It should be added to default config file if you use mongoid for the whole project.
Hope the explanation helpful.
Reference from origin gem source: https://github.com/mislav/will_paginate/wiki/Backwards-incompatibility at "WillPaginate::Collection" section.
P/S: this is just a work around if your query is not very large. If you want better performance, let's try mongoid-pagination gem, custom will_pagination gem or another pagination gem which supported Mongoid like kaminari.

A bit late, but for anyone else looking, I found 'will_paginate_mongoid'
https://github.com/lucasas/will_paginate_mongoid
Really straight forward and lets you simply do
collection.skip(20).limit(10)

Use the following gem.
Very helpful.
https://github.com/lucasas/will_paginate_mongoid

To update a bit the answers, now exists the Pagy gem, also much more performant (cpu/mem) than will_paginate and kaminari. This is a migration guide

silly thing, but it worked for me in sinatra after i added require 'mongoid-pagination'
to app.rb

Posting several years later, in case someone else faces the same challenge.
kaminari-mongoid was released in 2016, and is currently maintained.
https://github.com/kaminari/kaminari-mongoid
All the goodness of Kaminiari for Mongoid, including appropriate handling of Mongoid::Criteria results, which was possibly the cause of the OP's error.

Related

Sail. js / Waterline.js - find() not returning array

Problem: I'm getting unexpected output from code that previously worked.
Code Problem:
sails.models.user.find().then(function (users){...});
is currently returning { id: 1 }
but should return an array of User objects like [{id:x, name:y},...]
Code Alterations:
sails.models.user.find().exec(function (err, users){...}); does not contain an error and returns the same as using .then() like above.
sails.models.user.findOne(1).then(function (users){...}); correctly returns a User like {id:x, name:y}.
sails.models.venue.find().then(function (venues){...}); returns an array of venues, just as substituting any other class besides User.
Note:
This code was previously working (it's a pretty simple line), and the only changes I made between it working and not working was running npm install (but it was previously working on heroku where which installed, so I don't think that was a problem) and changing the schema of User to add a few columns (I did this by deleting the User table in the DB, updating the Sails User model, and lifting the app in create mode, so the table exactly matches the model). Neither of these should cause a problem, but we all know how "should" and coding don't mix :P
How do I fix this? And why did this happen? Thanks :)
Realized other code was calling the package sails-mock-models which was doing its job. Totally forgot about that code. Problem solved.

Trouble upgrading to new ember-simple-auth

G'day all,
I've been having trouble upgrading to a more recent version of the ember-simple-auth module.
In particular I seem to have two challenges:
1) the application no longer transitions to the desired route after authenticating. the configuration looks like this:
ENV['ember-simple-auth'] = {
crossOriginWhiteList: ['http://10.10.1.7:3000'],
routeAfterAuthentication: 'profile',
//store: 'simple-auth-session-store:local-storage',
//authorizer: 'simple-auth-authorizer:token',
};
but it never gets to "profile".
2) I can't get the authenticated session to stick after a reload. I had been trying to use the local-store which I believed would do the trick, but it's not. Has something changed in the implementation?
The documentation seems to indicate that the configuration strings are right, but the transition and session store don't seem to be working.
Has anyone had a similar problem?
Thanks,
Andrew
you could try adding "routeIfAlreadyAuthenticated" to ENV['ember-simple-auth'] - or you could transition manually in index route "afterModel" hook, if session is already authenticated
have you configured a session store? https://github.com/simplabs/ember-simple-auth#session-stores - the way it's configured changed in 1.0, now you can add the desired session store to app/session-stores/application.js - maybe this solves #1 too.
OK. As the comments call out, there were two problems here:
1) I had written a customer authorizer for the old version of simple-auth which didn't work with the new version, and
2) I had a typo in the adapter code, where DataAdapterMixin was DAtaAdapterMixin.
Removing (1) and fixing (2) fixed the problem.

Post a text request in Casablanca (C++ REST SDK)

I am writing a client side code in Visual C++ 2012 using C++ Rest SDK (codename "Casablanca").
I have a client created and wish to POST a text string to the server. However, when I send the following code, it is compiling but not sending sending the request.
When I remove everything after "methods::POST" and send a blank post request, then it is sent and received by the server.
Can you please guide me where the problem is. The documentation related to this function is available on Casablanca Documentation.
pplx::task<http_response>resp = client.request(methods::POST,L"",L"This is the random text that I wish to send", L"text/plain");
I think the usage you give here looks correct.
Is your Casablanca the latest version ? Please check that out from here : http://casablanca.codeplex.com/
If you are sure your measurement is accurate, you may want to create a minimal repro and file a bug here : http://casablanca.codeplex.com/workitem/list/basic
I was having a similar problem, all my POSTs was arriving in blank on server , after a few hours work above it, i found a possible solution.
I changed the default content type to application/x-www-form-urlencoded and I started to pass the values like this Example data=text1&data2=text2
client.request(methods::POST,L"",L"data=text1&data2=text2", L"application/x-www-form-urlencoded");
The body parameter must be a json::value.
I cannot comment yet so I have to put my thoughts in an answer. I solved this problem like this: There is an overload of the request method that takes as a parameter the content type so that you do not have to change the code.
m_client->request(methods::POST, L"/statuses/update.json?" + url_encode(data),L"",L"application/x-www-form-urlencoded");
Obviously you would have to implement the url_encode method but that is not difficult. There is a pretty good implementation in "Cassablanca". A search on this site will alos turn up some good examples.

implementing uploadify in kohana 3

I have a hard time implementing uploadify inside kohana 3, what changes should I make to the originial uploadify.php? just making it the index function in a controller doesn't work and returns a 500 error.
Thank you!
I'm not too sure if this will help, but I spent many hours dealing with uploadify the other day, and one of the tricks was to give back ANY response from the upload controller to let uploadify know that uploading is done.
Solution as simply as this: $this->request->response = "OK";
However, 500 states for interval server error, so the case might be also passing session ID with the 'scriptData' parameter when initializing uploadify, like this:
$('#file_upload').uploadify({'scriptData': {'<?php echo $session_name; ?>': '<?php echo session_id(); ?>'}});
Hope this helps, cheers!
Are you using Uploadify 2.1.x or 3.0 beta?
Anyway, you should be able to use any controller you want as long as you set the parameters in the javascript accordingly. And make sure you're not using a template for the controller dealing with the file uploads.
For instance, if you have a controller "files" and an action "uploadify" at URI /site/files/uploadify you should set the parameter to something like this:
'script' : '/site/files/uploadify',

Bug in SubSonic.Comparison.In

With the following code:
IEnumerable<int> LocalityIds = new List<int>();
PersonCollection pc =
new PersonCollection().
Where(Person.Columns.AddressLocalityId, SubSonic.Comparison.In, LocalityIds).
Load();
Although the initial collection is empty Subsonic still returns all records?!?!?!?!? Is this a bug or am i doing something wrong?
Thanks
I don't think we've setup collections to do this even though the Comparison is there. Have you tried Select().From().Where(..).In(LocalityIds).ExecuteAsCollection();
Just checked V2.2 and this still happens, when I have a bit of time I will try to submit a patch

Resources