I was thinking more about how to save all those options in the
database and still be able to manipulate them via activerecord.  One
thing I could do of course is to stream the options to yaml and store
that but that strikes as me as beeing a bit too rubyish. What I mean
is that if the data is to be used by something else (say a report
engine) then it won't do any good to store it in a yaml blob.

Another thing I though of was to use some database specific XML
thingie. I know postgres can store XML and query it with xpath but
again that strikes me as beeing a bit too specific.






On 6/7/06, Daniel Schierbeck <daniel.schierbeck / gmail.com> wrote:
> Tim Uckun wrote:
> > I have a table about an entity and there are a LOT of options about
> > this entitiy. For example isLeftHanded? isRedHaired? doesLikeRedRoses?
> > doesHatePolenta? etc.  What is the best way to keep these kinds of
> > binary information about an entity? Is there any harm in just adding
> > binary fields? Should I use a large number field and OR all the time
> > (does limit the number of options per field and makes for messy SQL
> > though). Should I have a text field with delimited data in it? Should
> > I join a separate table?
>
> Do you want a class to represent such an entity?
>
>    class Entity
>      def initialize
>        @opts = {:left_handed => false, :red_haired => true}
>      end
>
>      def method_missing(name, *args)
>        key = name.to_s[0..-2].to_sym
>        case name.to_s
>        when /\?$/
>          @opts[key]
>        when /=$/
>          @opts[key] = args.first
>        when /!$/
>          @opts[key] = !@opts[key]
>        else
>          super
>        end
>      end
>    end
>
>    entity = Entity.new
>    entity.red_haired?         => true
>    entity.red_haired!
>    entity.red_haired?         => false
>    entity.left_handed?        => false
>    entity.left_handed = true
>    entity.left_handed?        => true
>
> Furthermore, you could make the class smarter by allowing the options to
> be symbols that reference other options, i.e.
>
>    # `right_handed' will be the opposite of
>    # `left_handed'.
>    entity.right_handed = :left_handed!
>
>    entity.female = :male!
>
>    # `hippie' will have the same value
>    # as `long_haired'
>    entity.hippie = :long_haired
>
> And perhaps even allow procs:
>
>    entity.hippie = proc{|opts| opts[:long_haired] and
>                                opts[:likes_hendrix]}
>
> You could use #respond_to? to determine the type of the value.
>
>
> Cheers,
> Daniel
>
>