Also, if you're comfortable relying on auto validation (from `dm-validations`), the `validates_within` declaration can be collapsed into the property definition:
class SomeModel include DataMapper::Resource property :status, String, :set => %w[open closed unknown] end Chris: if you have something you're comfortable sharing, I'd love to see an example of how you're using dm-migrations with hand-written DDL. I'm about to embark on that soon myself, and working examples would be helpful! Thanks -- Emmanuel On Jun 27, 2011, at 3:16 AM, Chris Corbyn wrote: > If you just want a string with a restricted set of values, just define the > column as String and use a :validates_within validation rule: > > class SomeModel > include DataMapper::Resource > ... > property :status, String > > validates_within :status, :set => ['open', 'closed'] > end > > You'll have to write the migration yourself if you want that field to be > defined as a MySQL ENUM though (i.e. use dm-migrations and use the #execute > method to run the SQL). > > > On 27/06/2011, at 20:01, Pierre wrote: > >> Hello chris thanks for your answer >> >> Unfortunately I didn't find good doc for making my own types but I >> guess it would be easy to create an enum type which would be a string >> with a list of restricted values. >> >> The same for the type flags exept that values would be splited to an >> array when it gets out of the db and would be joined before it gets in >> the db. >> >> Does anyone know a good doc about creating types ? >> >> On Jun 26, 4:38 pm, Chris Corbyn <[email protected]> wrote: >>> I prefer to write my own migrations and expressly define the column as ENUM. >>> >>> Interesting, MySQL accepts both integer and string values for ENUMs (order >>> important): >>> >>> mysql> CREATE TABLE tbl ( >>> -> options ENUM('foo', 'bar') NOT NULL DEFAULT 'foo' >>> -> ); >>> Query OK, 0 rows affected (0.02 sec) >>> >>> mysql> >>> mysql> INSERT INTO tbl VALUES ('foo'), ('foo'), ('bar'); >>> Query OK, 3 rows affected (0.00 sec) >>> Records: 3 Duplicates: 0 Warnings: 0 >>> >>> mysql> SELECT * FROM tbl WHERE options = 1; >>> +---------+ >>> | options | >>> +---------+ >>> | foo | >>> | foo | >>> +---------+ >>> 2 rows in set (0.00 sec) >>> >>> mysql> SELECT * FROM tbl WHERE options = 2; >>> +---------+ >>> | options | >>> +---------+ >>> | bar | >>> +---------+ >>> 1 row in set (0.00 sec) >>> >>> mysql> >>> >>> On 26/06/2011, at 23:05, Pierre wrote: >>> >>> >>> >>> >>> >>> >>> >>>> Hello all, >>> >>>> Datamapper is great, but I don't understand how enum and flags works >>>> with mysql adapter. >>> >>>> First, they both create a integer field in the table which is annoying >>>> because it sould be respectively an enum field and a set field. >>> >>>> Then I don't understant how to work with these integer values. For >>>> exemple I'm triying to do something like that : >>> >>>> some_model = SomeModel.get(1) >>>> Description.all(:conditions => ['color_list = NULL OR color_list = ?', >>>> some_model.color]) >>> >>>> With color_list a Flag property and color an enum property from a >>>> model called some_model. It's not working. >>> >>>> Thanks, >>> >>>> Pierre >>> >>>> -- >>>> 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 >>>> athttp://groups.google.com/group/datamapper?hl=en. >> >> -- >> 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. >> > > -- > 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. > -- 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.
