A has_many B
validates :b, ? #
我究竟能检查到我至少有2个B?
答案 0 :(得分:3)
Rails 3?尝试:
validates :b, :length => { :minimum => 2 }
答案 1 :(得分:1)
从this answer你可以做到:
validate :has_at_least_two_potatoes?
def has_at_least_two_potatoes?
errors.add_to_base "Model must have at least 2 potatoes" if self.potatoes.size < 2
end