/ Published in: Rails
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# 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