2

さて、これは非常に奇妙な問題で、モデルが保存されている理由がわかりません。データ入力を別のレコードに正確に再作成し (同じレコードとの関係を確立しても)、重複したレコードは正常に保存されます。

クラス 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
4

0 に答える 0