【问题标题】:Datamapper 'belongs_to / has n' ignoring :required => falseDatamapper 'belongs_to / has n' 忽略:required => false
【发布时间】:2012-01-07 13:17:05
【问题描述】:

我最近继承了一个使用 datamapper 作为其 orm 的旧 merb 应用程序。我对 DM 不太熟悉,所以我可能忽略了一些明显的东西。我有两个模型之间的简单关系,不是必须的,如下:

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"]

只是为了确保我也尝试在“有 n”方面设置 :required => false 但它当然没有任何区别。

所有这些都在 merb 1.0.13 和 datamapper 0.10.1 上


编辑:下面的答案是正确的,但我确实找到了解决方法,您可以添加 :nullable => true 以使其工作,如下所示:

belongs_to :upload, :required => false, :nullable => true

【问题讨论】:

    标签: datamapper has-many merb


    【解决方案1】:

    旧 DM 中曾经存在一个导致此行为的错误。我强烈建议将此应用程序移植到最近的 Rails 和 DM。

    【讨论】:

      猜你喜欢
      • 2011-11-11
      • 2013-04-15
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      相关资源
      最近更新 更多