Syed Raza
Syed Raza

Reputation: 1098

Rails Active Record Validation condition base

class Material < ActiveRecord::Base
  belongs_to :material_type
  belongs_to :product_review
  validates :url, :presence => true, :if => :url_presence?
  validates :video, :presence => true, :if => :video_presence?
  def url_presence?
    if !self.title.blank? and self.material_type.title.eql? :url
      true
    end
  end

  def video_presence?
    if !self.title.blank? and self.material_type.title.eql? :video
      true
    end
  end


  has_attached_file :video,
                    :url => "/system/video/:id/:attachment/:style/:basename.:extension",
                    :path => ":rails_root/public/system/video/:id/:attachment/:style/:basename.:extension",
                    :default_url => "/image/video.png"
end

I assumption if its finds Title fields fill and material_type is url than it perform validation for url field presence validation check, but it not helpful

Upvotes: 0

Views: 689

Answers (1)

rdvdijk
rdvdijk

Reputation: 4398

I think you need to compare strings, not symbols.

validates :url, :presence => true, :if => :url_present?
validates :video, :presence => true, :if => :video_present?

def url_present?
  self.title.present? and self.material_type.title == "url"
end

def video_present?
  self.title.present? and self.material_type.title == "video"
end

Upvotes: 1

Related Questions