So Candy is a really simple library for interacting with Mongo in Ruby.
My poor SQL brain is having a tough time figuring out how I should map out this problem:
There are users, there are things. Each thing was made by one user, but should be accessible to a subset of all users (specified within the thing). Leaving the specification of user out of the way for now, how would I get a list of all things that user X has access to?
class Thing
include Candy::Piece
end
class Things
include Candy::Collection
collects :thing
end
Should I assign the allowed users to a thing like this? (lets just use strings to reference users for now)
t = Thing.new
t.allowed = ['X','Y','Z']
This seems about right to me, which would make me want to do:
Things.find(allowed:'X')
but it’s not quite working…
NoMethodError: undefined method ‘call’ for {:allowed=>"X"}:Hash
any ideas?
I’m really sorry I took so long to catch this and respond. This might be too late for your purposes, but:
Candy doesn’t implement a
findmethod. This is on purpose: if an object represents a collection, every access is implicitly finding something in that collection. It’s the same reason there is nosavemethod. If the mapping is truly transparent, verbs that mean “Do this in the database” shouldn’t be necessary.So to do what you want, you could either just make a new Things object with the scope passed on creation:
…or you could save a step and do it by class method:
…or you could start with the whole collection and limit it by attribute later:
So… Um. All of those will work. But. I have to warn you that I’m really not happy with the general usability of Candy right now, and of collections and array fields in particular. The biggest problem is accessors: the
[]method isn’t working like you’d expect, so you end up having to callto_aandrefreshand other things that feel sticky and unpleasant.This needs to be fixed, and I will do so as soon as I finish the driver rewrite (a related project called Crunch). In the short term, Candy is probably best viewed as an experiment for the adventurous, and I can’t guarantee it’ll save time until the interface is locked down a bit better. I’m sorry about that.