在 rails 上的 ruby 中使用小于或大于验证

Using less than or greater validation in ruby on rails

我有两个模型,第一个模型是条目,它与我的控制器条目一起使用以创建新请求。我需要验证 range_days 来自条目小于或等于我第二个模型的范围empaccrl..但无论我做什么仍然得到一个未定义的方法范围...

我的入门模型

class Entry < ActiveRecord::Base


  self.primary_key = 'id' 

  validates :indirect_id, presence: true, allow_blank: false

  validates :leave_range, presence: true, allow_blank: false

  validates :range_days, presence: true, allow_blank: false, length: {    maximum: 2 }

  # Really wish I could get this validation to work ..............

  validates :range_days, :numericality => { :less_than_or_equal_to => :range }, :presence => true

  belongs_to :empaccrl


 attr_accessible :emp_id, :person_id, :emp_dept, :emp_no, :emp_first_name, :emp_last_name, :emp_mail_addr, :indirect_id, :mgr_no, :mgr_first_name, :mgr_last_name, :mgr_mail_addr, :leave_range, :employee_type, :seq_no, :range_days, :alt_mgr_name, :alt_mgr_addr, :alt_mgr_no  

# This is for the validation 
def range
  empaccrl.range
end
end

我的 Empaccrl 模型

class Empaccrl < ActiveRecord::Base

  establish_connection :vdev

  self.table_name = 'empaccrl'

  belongs_to :entry 

  attr_accessor :range 
 end     

我的入口控制器有创建方法和新的

 def new
   @entry = Entry.new
   respond_to do |format|
     format.html# new.html.haml
     format.xml { render :xml => @entry }
   end
 end

 def create
   params.permit!
   @entry = Entry.new(params[:entry])    

   respond_to do |format|

    if @entry.save
      EntryMailer.submit_for_approval(@entry).deliver
      format.html { redirect_to(entry_path( @entry ), :notice => 'Entry successfully created.') }
      format.xml { render :xml => @entry, :status => :created, :location => @entry }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @entry.errors, :status => :unprocessable_entity }
    end
end

结束

这是我的参赛作品table

      ID    NUMBER(38,0)    
      CREATED_AT    DATE        
      UPDATED_AT    DATE        
      EMP_ID    VARCHAR2(255 BYTE)
      EMP_NO    VARCHAR2(255 BYTE)  
      EMP_FIRST_NAME    VARCHAR2(255 BYTE)  
      EMP_LAST_NAME VARCHAR2(255 BYTE)  
      EMP_MAIL_ADDR VARCHAR2(255 BYTE)  
      INDIRECT_ID   VARCHAR2(255 BYTE)  
      MGR_NO    VARCHAR2(255 BYTE)  
      MGR_FIRST_NAME    VARCHAR2(255 BYTE)
      RANGE_DAYS    NUMBER(38,0)        

这是我的 Empaccrl table

     PERSON_ID  NUMBER(10,0)    
     IS_ACTIVE  VARCHAR2(1 BYTE)
     ACCRUAL_DATE   DATE    
     RANGE  NUMBER(10,0)    
     PROJECTED_ACCRUED  NUMBER(12,5)

非常感谢任何帮助我盯着这个看了这么久眼睛都在发烫!!!!!!

无法使用内置验证来执行此操作。您需要编写一个自定义的:

validates :range_days, :presence => true
validate :range_days_smaller_than_range, if: :range

private

def range_days_smaller_than_range
  errors.add(:range_days, "LOL Nah! <Any error message or translation key here>") if range_days > range
end 

我成功了!!!! 这就是我让它做我想做的事情的方法,感谢@BroiSatse 的帮助!

validate :range_days_smaller_than_range, if: :range

def range
  range = Empaccrl.where("person_id = ? and is_active = ?", '14052', 'Y').pluck(:range).first
end

private

def range_days_smaller_than_range
  errors.add(:range_days, "Sorry you don't have days for this request") if range_days > range
end

结束