开发者

Ruby, Candy and SQL-like mongo stuff

开发者 https://www.devze.com 2023-01-11 01:12 出处:网络
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:

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 ref开发者_StackOverflow社区erence 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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消