Return to Snippet

Revision: 8783
at October 7, 2008 15:20 by filiptepper


Initial Code
# 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')
>> e = Email.new('email' => '[email protected]')
>> 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 = Email.new('email' => '[email protected]')
>> e.client = c
>> c.save!
=> true

Initial URL


Initial Description


Initial Title
has_many validates_presence_of

Initial Tags
ruby

Initial Language
Rails