Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Complex queries to get an object

dmathieu edited this page Sep 23, 2010 · 1 revision

By default, Rediline will get your object using Object.find(id).
However in some cases (like, for exemple, embedded documents in a NoSQL database), this can't work.

To solve that problem, you can specify a specific object to be queried an other way.
Let's see for example, if we have a Post object, which has several embedded Comments with mongoid.

class Comment
    include Mongoid::Document
    include Rediline::Object
    
    rediline :timeline,
        :post => lambda {|c| c.post },
        :queries => {
            :object => lambda {|o, id| o.post.comments.find(id) }
        },
        :when => :after_create

Two things are important here :

:post => lambda {|c| c.post }

We store the post object so we'll be able to find it later.
You can store any object you cant as long as they have an id and you can retrieve them later with Object.find(id).

:queries => {
    :object => lambda {|o, id| o.post.comments.find(id) }
}

By default, rediline will directly search for the object. But by specifying this, you can provide it a different way to query for these objects.
You can specify any object in queries hash. For example you could the following (which would not be very useful though) :

:queries => {
    :post => lambda {|o, id| Post.find(1) }
}

Which will just ignore the id and always retrieve the post with id=1.

Clone this wiki locally