Question

I recently inherited an old merb app that uses datamapper as its orm. I'm not too familliar with DM so there is probably something obvious I'm overlooking. I have a simple relationship between two models, which is not required, as follows:

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

But for some reason it won't let me save a user without an attached upload:

> u = User.create :name => 'foo'
 => #<User @id=nil @name=nil @upload_id=nil>
> s.errors.full_messages
 => ["Upload must not be blank"]

Just to be sure I tried setting :required => false on the "has n" side too but it made no difference of course.

All this on merb 1.0.13 and datamapper 0.10.1


EDIT: The answer below is correct but I did find a workaround, you can add :nullable => true to make this work, as follows:

belongs_to :upload, :required => false, :nullable => true
Was it helpful?

Solution

There used to be a bug in old DM which was causing this behavior. I'd strongly suggest porting this app to recent Rails & DM.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top