Implementing data partitioning in ActiveRecord
One of the things I often need to do with a project is partition the data by some abstract method. Sometimes it’s based on the user who created it, sometimes some outside element, such as a sensor. Doing this traditionally has been a royal pain in the ass. Earlier I mentioned scoped_access and how it might work for doing general access control.
This is just a “rough thought,” but you should be able to extend the general security model (user, roles, rights) with a another pair of objects. What they’re called, I’m not sure yet, but let’s call them a “group” and a “partition”. That means you have something like this:

Tying all these together is a HASBTM relationship that everyone can figure out. So, what is a partition? Well, a partition is simply an extension of using scoped_access (or with_scope), with something silly like this going on:
Event.with_scope(:find => {
:conditions => "sensor_id = 12"}) do
...
end
It’s really that simple, I think. So how do you store them? well, I think realistically, store a single constraint per partition and then combine them at the group level. Storing multiple constraints in a single partition comes with all sorts of increased complexity in my mind that doesn’t really gain you much.
This entry was posted at 10:31 pm on 30 March 2006 and is filed under Ruby. You can follow any responses to this entry through the post-specific RSS 2.0 feed.
No comments found.
Both comments and pings are currently closed.