DataMapper での一意制約の指定方法
DataMapper で一意制約 (unique constraint) をつける方法がドキュメントに見つからないと思った人はおらんかー?
DataMapper では、一意制約には :unique_index オプションをつければよい。
class Department include DataMapper::Resource property :id, Serial ## 単一プロパティに一意制約をつけたいとき property :name, String, :nullable=>false, :unique_index=>true ## 複数のプロパティにまたがって一意制約をつけたいとき (:u1 は任意) property :parent_id, Integer, :nullable=>false, :unique_index=>:u1 property :code, String, :nullable=>false, :unique_index=>:u1 ## ちなみに :u1 を "u1" と指定すると無限ループになるので注意 ## http://datamapper.lighthouseapp.com/projects/20609/tickets/637 end
・・・と思っていたんだけど、DataMapper の BTS にこんな ticket が。。。
Currently you pass in a :unique and :unique_index options when declaring properties. The :unique option sets up validation on the property, while the :unique_index sets up the actual unique indexes when using auto_migrate!
I think that :unique_index should be deprecated and the functionality should be rolled into :unique. It makes no sense to use one and not the other as far as I can tell. In fact it could be confusing if you specified :unique_index and forget :unique, and then get DB errors.
#403 Unify unique and unique_index property options - DataMapper - datamapper
なんと、:unique_index は index を作るだけであり、validation をするには :unique も指定しないといけないのだとか。
うわーん、知らなかったよー。
これはいけてないわ。互換性がなくなるのは許すから、:unique_index と :unique を統合してほしい。
Ruby で複文を書く方法
Ruby では「( )」で複文が書けるそうな。
つまり、(statement; statement; statement) みたいなことができるらしい。
括弧の中で改行があるとそれは独立した式とみなされます。括弧の中でも改行はセミコロンを置いたのと同じ意味になります。
Re: 複数行にわたる式の評価
実際にやってみた。
### 文をセミコロンで区切る irb> (puts 123; puts "foo"; puts true) 123 foo true => nil ### 文を改行で区切る irb> (puts 456 >> puts "bar" >> puts false >> ) 456 bar false => nil
ほんまやー!
みんな知ってた? 自分は全然知らんかった。
もしかして、今までこんな感じで書いていたのが
found = false while line = gets() if line =~ pattern found = true break end end
1 行にまとめてこう書けるということか?
found = false while line = gets() (found = true; break) if line =~ pattern end
うへー、知らんかった。
便利っちゃあ便利だけど・・・微妙。間違った '(' を書いてしまったら、syntax error が見つけにくいだろうなあ。ないほうがいいような気はする。少なくとも、改行が「;」と同じ扱いになるという仕様は、間違い探しがしにくくなるので廃止したほうがいいと思う。