complex ActiveRecord to MongoID query

121 views
Skip to first unread message

Alexey Poimtsev

unread,
Sep 15, 2011, 7:55:46 AM9/15/11
to Mongoid
could you please help me to convert active record query to mongoid?

where(["access_grants.access_token = ?
AND (access_grants.access_token_expires_at IS NULL
OR access_grants.access_token_expires_at > ?)",
conditions[token_authentication_key],
Time.now]).joins(:access_grants).
select("users.*").first

sorry for n00b question :)

Durran Jordan

unread,
Sep 15, 2011, 8:31:49 AM9/15/11
to mon...@googlegroups.com
This query has a join in it, you can't do the equivalent in MongoDB.

2011/9/15 Alexey Poimtsev <alexey....@gmail.com>

Alexey Poimtsev

unread,
Sep 15, 2011, 8:59:48 AM9/15/11
to Mongoid
huh ... for better understanding - i'm migrating code from this
article (http://www.railsatwork.com/2010/10/implementing-oauth-
provider-part-1.html) AR --> MongoId. My code example from User model
from provider and used to find appropriate User. In this case i don't
need joins, but need for correct query to mongodb :)

On Sep 15, 4:31 pm, Durran Jordan <dur...@gmail.com> wrote:
> This query has a join in it, you can't do the equivalent in MongoDB.
>
> 2011/9/15 Alexey Poimtsev <alexey.poimt...@gmail.com>

Durran Jordan

unread,
Sep 15, 2011, 10:00:54 AM9/15/11
to mon...@googlegroups.com
That query cannot be done in MongoDB. The example article you linked to is trying to find users based on values in the access_grants table via a join. If you modeled this the same way you would have a users collection and an access_grants collection in MongoDB and it could not be accomplished in a single query, you would have to do it in 2.

user_id = AccessGrant.where(:access_token => conditions[:token_authentication_key]).any_of({ :access_token_expires_at => nil }, { :access_token_expires_at.gt => Time.now).first.user_id
user = User.find(user_id)

2011/9/15 Alexey Poimtsev <alexey....@gmail.com>

Alexey Poimtsev

unread,
Sep 15, 2011, 5:47:11 PM9/15/11
to Mongoid
thanks, man!

On Sep 15, 6:00 pm, Durran Jordan <dur...@gmail.com> wrote:
> That query cannot be done in MongoDB. The example article you linked to is
> trying to find users based on values in the access_grants table via a join.
> If you modeled this the same way you would have a users collection and an
> access_grants collection in MongoDB and it could not be accomplished in a
> single query, you would have to do it in 2.
>
> user_id = AccessGrant.where(:access_token =>
> conditions[:token_authentication_key]).any_of({ :access_token_expires_at =>
> nil }, { :access_token_expires_at.gt => Time.now).first.user_id
> user = User.find(user_id)
>
> 2011/9/15 Alexey Poimtsev <alexey.poimt...@gmail.com>
Reply all
Reply to author
Forward
0 new messages