-
Notifications
You must be signed in to change notification settings - Fork 0
Complex queries to get an object
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.