MarkMT wrote:
> The wiki documentation on Enum properties -
>
> http://datamapper.org/doku.php?id=dm-more:dm-types
>
> shows an example in which the enumerated property values are symbols,
> e.g. Enum[:new, :open, :closed, :invalid].
>
> Using symbols seems kind of appealing when you don't need to use the
> value for any purpose other than distinguishing between the options
> (e.g. you don't need to do anything with a string value like 'new'
> etc).
>
> However, I notice that if I'm creating or finding a db record based on
> form input, the form is going to present it's value as a string. So if
> I have a class 'User' with property 'member_class' defined as Enum
> [:full, :associate], and I submit a form for a user that has a field
> named 'member_class', I can't do something like -
>
> u = User.first(:member_class => params[:member_class])
>
> because the parameter is a string, not a symbol.
>
> I'm just wondering if there is any simple way to get around this. At
> the moment I'm using strings in my Enum definition, which is fine, but
> as I say, I can see the appeal of using symbols and short of using a
> case statement to convert string parameters to symbols, wonder if
> there is a way to do this. And if not, I wonder whether there is
> really any context in which it does make sense to use symbolic Enums.
>
> Mark.
>   
Mark,

You could use to_sym convert a string to a symbol.  So perhaps something 
like this will work:

u = User.first(:member_class => params[:member_class].to_sym)

Earle


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DataMapper" 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/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to