On 20 October 2011 18:14, Robert Walker <[email protected]> wrote: > Peter Hicks wrote in post #1027558: >> I have some data that could be a combination of around 30 values. >> Typical >> values could be 'TB', 'TF', 'D', 'U', '-D', 'OP', 'RM', or nil. >> >> Ideally, I'd use the SET data type in PostgreSQL, but ActiveRecord >> doesn't >> appear to support[1] this. More specifically, I can run a migration on >> my >> development database, but db/schema.rb contains a comment about an >> unsupported data-type. >> >> I've considered creating 30 columns to model this, however I'm not >> convinced >> that's efficient. I've also thought about separating each value with a >> : >> and storing this - for example, ":TF:-D:" - whilst I can query for >> "WHERE >> foo LIKE '%:D:%'", that probably isn't indexable as it's a text search. >> >> Can anyone help me to come up with an efficient way to do this in Rails >> 3.0.10? > > Yes. store this as a bit-mask. Then your query is as efficient using > integer comparison. Map your labels to the bit-mask. > > Example: > > Constants > ----- > TB => 1 > TF => (1 << 1) > D => (1 << 2) > U => (1 << 3) > MINUS_D => (1 << 4) > > select * from bar where foo = (TB | D); > or > select * from bar where foo = 5;
That works of course, but then you may have the complication of adding all the setters and accessors so you can say things like widget.tb = true do_something if widget.tb and so on. Once again it depends upon the details of the OPs requirement. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

