是否有多个验证存在
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了是否有多个验证存在相关的知识,希望对你有一定的参考价值。
# migrations class CreateClients < ActiveRecord::Migration def self.up create_table :clients do |t| t.string :name, :length => 255, :null => false t.timestamps end end end class CreateEmails < ActiveRecord::Migration def self.up create_table :emails do |t| t.integer :client_id t.string :email, :length => 64, :null => false t.timestamps end end end # model class Email < ActiveRecord::Base belongs_to :client validates_presence_of :email validates_presence_of :client_id end class Client < ActiveRecord::Base has_many :emails end # ./script/console >> c = Client.new('name' => 'Client') >> c.emails << e >> c.save! # ActiveRecord::RecordInvalid: Validation failed: Emails is invalid >> e.errors.full_messages => ["Client can't be blank"] # ./script/console >> c = Client.new('name' => 'Client') >> e.client = c >> c.save! => true
以上是关于是否有多个验证存在的主要内容,如果未能解决你的问题,请参考以下文章