Modelling Unique Collections in Rails/SQL

I’m puzzelling over how to model for want of a better description
unique collections in Rails and SQL

Heres my Domain Specific Example:

I start with…

class Band
has_many :line_ups # ?
end

class LineUp
belongs_to :band #?
end

class Recording
belongs_to :line_up # ?
end

class Tour
belongs_to :line_up # ?
end

But I’m struggling to fully model the relationship between Bands,
LineUps and People in Rails and SQL

LineUps are defined/distinct/unique from one another by the collection
of People in them.

Any ideas ?

Tony

[email protected] wrote:

end

But I’m struggling to fully model the relationship between Bands,
LineUps and People in Rails and SQL

LineUps are defined/distinct/unique from one another by the collection
of People in them.

Ick. Somehow, I prefer SQL as a notation for relational data models
instead of Rails annotations - especially since I thought the whole
purpose of the magic and shenanigans was to only require them when
inferring the DB structure isn’t possible.

This isn’t a constraint a simple relational model can express, I’d say.
I personally don’t think you specify it short of using an on update /
insert trigger and a very, very convoluted join to find out if there’s a
full outer join of the sets of people of any pair of distinct lineups
that has no null values on either side.

(Maybe the above is possible in a more elegant way. Either way, I’d love
to see either the on insert trigger that verifies that constraint, or
the alternative solution - convoluted SQL is fun reading :P)

Either that, or you could use a validator, except doing this in-process
would do Horrible Things to performance unless the lineups are
read-mostly (preferrably never modified, only added), and you’re caching
things accordingly, give-or-take prefetching the LineUps into the cache
on application startup.

(I’d also be mildly interested on just how you’d do that on Rails, but
that’s veering into the scope of their mailing list too much and I fear
the redirect.)

Any ideas ?

Personally, I wouldn’t bother. It is entirely possible that a band
assumes the identical lineup twice in its history. I’d presume your
model also holds dates of when a lineup was formed / disbanded, it
should be sufficient to verify overlap of those dates. If you’re using
separate Person records, the memory overhead to keep multiple
collections of those is completely negligible compared to what you store
in varchars anyway.

And to store dates of forming / disbanding of a lineup and enforce that
constraint, you’d have to compensate for the possibility of two
identical lineups for a band at different points in time by decomposing
LineUp into something like LineUpPeriod and PeopleSet, and then the
whole thing would go pear-shaped. Highly normalised relational models do
NOT map nicely to ActiveRecord and the API would turn baroque and
basically you’d be writing mostly raw SQL using Ruby as an alternate
syntax - something like RBatis, where your OO data model is decoupled
from the SQL structure would serve you better.

David V.

Any ideas ?Personally, I wouldn’t bother.

Obviously not the answer I was looking for.
:slight_smile:

It is entirely possible that a band
assumes the identical lineup twice in its history. I’d presume your
model also holds dates of when a lineup was formed / disbanded, it
should be sufficient to verify overlap of those dates. I

Unfortunately Line Ups don’t have start / end dates. Its more complex /
simpler that that.
LineUps change all the the time from gig to gig, recording to to
recording so I’m thinking a LineUps should just be a unique collection
of People tied to a band_id

How can that be modelled in SQL ?

A unique composite field of people ids ?

Tony