Datamapper资源验证失败但错误为空

时间:2013-03-06 09:43:33

标签: ruby datamapper ruby-datamapper

好的,这是一个非常奇怪的问题,我无法弄清楚为什么要保存模型。我已经在另一条记录中完全重新创建了数据输入(甚至建立了与相同记录的关系),并且重复记录保存得很好。

当我去保存类Studio时,save失败,但验证错误哈希是空的。 Studio看起来像这样(为了简洁起见,我已经删除了一些类方法):

class Studio
  include DataMapper::Resource

  # attributes
  property :id, Serial
  property :name, String, :required => true, :length => 255
  property :short_description, Text
  property :long_description, Text
  property :rating, Numeric
  property :accommodation, Boolean, :default => true
  property :vegan, Boolean, :default => true
  property :vegetarian, Boolean, :default => true
  property :forest, Boolean, :default => true
  property :mountain, Boolean, :default => true
  property :beach, Boolean, :default => true
  property :pool, Boolean, :default => true
  property :phone_1, String
  property :phone_1_prefix, String
  property :phone_2, String
  property :phone_2_prefix, String
  property :fax, String
  property :fax_prefix, String
  property :phone_1_international_dialling_code, String
  property :phone_2_international_dialling_code, String
  property :fax_international_dialling_code, String
  property :web, String, :format => :url, :required => true, :length => 255
  property :contact_name, String, :length => 255
  property :contact_email, String, :format => :email_address
  property :created_at, DateTime
  property :updated_at, DateTime
  property :details_complete, Boolean, :default => false
  property :status, Enum[:active, :pending, :disabled, :archived, :deleted], :default => :active, :required => true
  property :notes, Text
  property :course_schedule_url, String, :length => 255
  property :permalink, String, :length => 500
  property :signup_token, String, :length => 255
  property :founder_info, Text
  property :photo_rating, Numeric
  property :accepting_bookings, Boolean, :default => true

  # relationships
  belongs_to :location
  belongs_to :owner, :model => User, :required => false
  belongs_to :submitter, :model => User, :required => false

  has n, :courses, :order => [:display_order.asc, :start_date.asc, :is_recurring.asc], :status => :active, :constraint => :destroy  
  has n, :images, :through => Resource, :constraint => :skip
  has n, :subcategories, :through => Resource, :constraint => :skip  
  has n, :bookings, :constraint => :set_nil
  has n, :reviews, :status => :active, :constraint => :set_nil

  validates_presence_of :phone_1, :if => lambda { |c| (!c.phone_1_prefix.nil? && !c.phone_1_prefix.empty?) }
  validates_presence_of :phone_1_prefix, :if => lambda { |c| (!c.phone_1.nil? && !c.phone_1.empty?) }
  validates_presence_of :phone_2, :if => lambda { |c| (!c.phone_2_prefix.nil? && !c.phone_2_prefix.empty?) }
  validates_presence_of :phone_2_prefix, :if => lambda { |c| (!c.phone_2.nil? && !c.phone_2.empty?) }
  validates_presence_of :fax, :if => lambda { |c| (!c.fax_prefix.nil? && !c.fax_prefix.empty?) }
  validates_presence_of :fax_prefix, :if => lambda { |c| (!c.fax.nil? && !c.fax.empty?) }

  # hooks
  before :save, :create_permalink

  def create_permalink
    # remove punctuation
    permalink = self.name.gsub(/[ ]/, '-').gsub(/[^0-9A-Za-z-]/, '')

    self.subcategories.each do |sub|
      unless (permalink.length + sub.name.gsub(/[ ]/, '-').gsub(/[^0-9A-Za-z-]/, '').length) > 255
        permalink += "-" + sub.name.gsub(/[ ]/, '-').gsub(/[^0-9A-Za-z-]/, '')
      end
    end
    permalink.downcase!
    self.permalink = permalink
  end

end

0 个答案:

没有答案