【问题标题】:Validate assosiations rails mongomapper验证关联 rails mongomapper
【发布时间】:2014-09-04 07:38:32
【问题描述】:

如何验证关联

我有广告客户 belongs_to 城市 我需要检查城市的唯一性名称

我的模型的大块

  key :name, String , :required => true ,:unique => true ,:scope => :city_id
  key :city_id, ObjectId   , :required => true


  belongs_to :city

我试过了

  key :name, String , :required => true ,:unique => true ,:scope => :city_id

我错了

【问题讨论】:

    标签: ruby-on-rails validation mongomapper


    【解决方案1】:

    除了你所拥有的,你可以放:

    key :name, String, :required => true
    

    另外一行:

    validates :name,  :uniqueness => {:scope => :city_id}
    

    【讨论】:

    • +1 也可以使用 rails presence helper 代替必需属性 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 2014-10-24
    • 2011-08-07
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多