最近、datamapper を orm として使用する古い merb アプリを継承しました。私はDMにあまり慣れていないので、見落としている明らかな何かがあるかもしれません. 次のように、2 つのモデル間に単純な関係がありますが、これは必須ではありません。
class User
include DataMapper::Resource
property :id, Serial
property :name, String, :length => 100, :nullable => false
belongs_to :upload, :required => false
end
class Upload
include DataMapper::Resource
property :id, Serial
property :filename, String
has n, :users
end
しかし、何らかの理由で、アップロードが添付されていないユーザーを保存することはできません:
> u = User.create :name => 'foo'
=> #<User @id=nil @name=nil @upload_id=nil>
> s.errors.full_messages
=> ["Upload must not be blank"]
念のため、「has n」側でも :required => false を設定しようとしましたが、もちろん違いはありませんでした。
これはすべて merb 1.0.13 と datamapper 0.10.1 で
編集: 以下の答えは正しいですが、回避策を見つけました。次のように :nullable => true を追加して、これを機能させることができます。
belongs_to :upload, :required => false, :nullable => true