【发布时间】:2015-03-03 15:03:00
【问题描述】:
我有两个模型,第一个模型是条目,它与我的控制器条目一起用于创建新请求。我需要验证条目中的 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
我的入口控制器带有 create 方法和 new
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
结束
这是我的入口表
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 表
PERSON_ID NUMBER(10,0)
IS_ACTIVE VARCHAR2(1 BYTE)
ACCRUAL_DATE DATE
RANGE NUMBER(10,0)
PROJECTED_ACCRUED NUMBER(12,5)
非常感谢任何帮助,我的眼睛盯着这个看这么久!!!!!
【问题讨论】:
-
我认为你在 :range 中不需要冒号,试试 => range
-
这就是我尝试的结果.. @RailsOuter undefined local variable or method `range' for #<0x0000000621d418>0x0000000621d418>
标签: ruby-on-rails oracle validation ruby-on-rails-4 activerecord