2011-03-15 39 views

回答

-1

你可以創建自己的方法和回調。

class ShoppingCart < ActiveRecord::Base 
    has_many :items 
    before_validation :check_items 

    def check_items 
    items.each do |item| 
     # check if valid and add to errors if any 
    end 
    return false if errors.any? # this will cause save to return false 
    end 
end 

http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

+0

假設你正在使用ActiveRecord – lebreeze 2011-03-15 07:50:45

+0

這是後話,將工作。我想要「驗證」的香草方式http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates。我將使用validates_associated,直到那時。 – Neelesh 2011-03-17 05:02:26