Ruby on rails 如何使用“验证属性”;验证:属性";是否引用父对象属性的值?

Ruby on rails 如何使用“验证属性”;验证:属性";是否引用父对象属性的值?,ruby-on-rails,ruby-on-rails-3,validation,customvalidator,Ruby On Rails,Ruby On Rails 3,Validation,Customvalidator,在O'Reilly的“头优先轨道”(ed.2009)的这个练习中,有两个相关对象 “Flight”对象[我使用注释gem来显示每个属性]: # == Schema Information # # Table name: flights # # id :integer not null, primary key # departure :datetime # arrival :datetime # desti

在O'Reilly的“头优先轨道”(ed.2009)的这个练习中,有两个相关对象

“Flight”对象[我使用注释gem来显示每个属性]:

# == Schema Information
#
# Table name: flights
#
#  id                :integer         not null, primary key
#  departure         :datetime
#  arrival           :datetime
#  destination       :string(255)
#  baggage_allowance :decimal(, )
#  capacity          :integer
#  created_at        :datetime
#  updated_at        :datetime
#

class Flight < ActiveRecord::Base
  has_many :seats
end
但它会导致SeatController中出现名称错误

undefined local variable or method `flight' for #<Class:0x68ac3d8>"
但它抛出了一个命名错误异常:

undefined method `flight' for #<Class:0x67e9b40>
未定义的方法“flight”#
有没有办法让更漂亮的验证器工作

进行这种验证的最佳实践是什么

多谢各位

---编辑---

正如毛里西奥·林哈雷斯(Maurício Linhares)在下文中善意地建议的那样,定义一个:装袋津贴符号是可以解决问题的。 我想更好地了解真正需要自定义验证的地方。 这一个呢,是否可以将其转换为“validates”方法?为什么

class Seat < ActiveRecord::Base
.
.
.
def validate
  if flight.capacity <= flight.seats.size
    errors.add_to_base("The flight is fully booked, no more seats available!")
  end
end
class-Seat如果flight.capacity您可以这样做:

class Seat < ActiveRecord::Base

  validates :baggage, :numericality => { :less_than_or_equal_to => :baggage_allowance }, :presence => true 

  belongs_to :flight

  def baggage_allowance
    flight.baggage_allowance
  end  
end
class-Seat{:小于或等于=>:baggage\u Allowment},:presence=>true
属于:航班
行李限额
航班行李限额
结束
结束
编辑

你不能这样做:

class Seat < ActiveRecord::Base
        validates :baggage, :numericality => { :less_than_or_equal_to => flight.baggage_allowance }, :presence => true 
end
class-Seat{:小于或等于等于flight.baggage\u Allowment},:presence=>true
结束

因为方法验证是在类级别调用的,所以没有可用的flight变量,因为它是一个实例变量。当您将其配置为:baggage_-allowment时,您会告诉Rails在座椅的实例上调用:baggage_-allowment方法,以便能够访问该值。

尝试验证程序中的匿名函数:
:less_-than_或_-equal_-to=>Proc.new{flight | flight.baggage_-allowment}
我写道:
验证:baggage,:numericality=>{:less_than_或_equal_to=>Proc.new{flight{flight}flight.baggage_allowment},:presence=>true
它抛出:
NoMethodError in SeatsController#为#创建未定义的方法“baggage_allowment”
谢谢!谢谢你,这很有效!因此,在处理“validates”时,父模型属性似乎没有初始化。有没有具体的原因?你知道我在哪里可以学到更多关于它的知识吗?你的代码工作正常,解决了我的问题!我只想了解更多关于“validates”方法的知识,以了解何时无法避免自定义验证(这有损于代码的清晰性)。换句话说,我想知道为什么不能调用父方法,如
中的“
航班。行李限额
”验证
。我编辑了第一篇文章,以便更好地解释我的担忧。再次感谢你。好了,回答补充道。
undefined method `flight' for #<Class:0x67e9b40>
class Seat < ActiveRecord::Base
.
.
.
def validate
  if flight.capacity <= flight.seats.size
    errors.add_to_base("The flight is fully booked, no more seats available!")
  end
end
class Seat < ActiveRecord::Base

  validates :baggage, :numericality => { :less_than_or_equal_to => :baggage_allowance }, :presence => true 

  belongs_to :flight

  def baggage_allowance
    flight.baggage_allowance
  end  
end
class Seat < ActiveRecord::Base
        validates :baggage, :numericality => { :less_than_or_equal_to => flight.baggage_allowance }, :presence => true 
end