tags:

views:

43

answers:

1

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?

+1  A: 

Hi -- 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 find method. 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 no save method. 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:

x_is_allowed = Things.new(allowed: 'X')

...or you could save a step and do it by class method:

x_is_allowed = Things.allowed('X')

...or you could start with the whole collection and limit it by attribute later:

things = Things.new
x_is_allowed = things.allowed('X')

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 call to_a and refresh and 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.

SFEley
No problem at all! I've been on holiday so it's barely been any time at all for me :P This sounds fantastic - I'm investigating using Candy for a reasonably big (personal) project, I know it's a bad idea to consider Candy given your warning about the impermanence of it's API but it's so tasty! I'll certainly be using it for the testing phase, as the DB interaction is minimal and Candy is whole lines simpler! Let me know if there's any way I can help with it's development. Thanks candy, thandy.
JP