Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Migration - 008_create_actors.rb
- class CreateActors < ActiveRecord::Migration
- def self.up
- create_table :actors do |table|
- table.column :name, :string, :null => false, :limit => 255
- table.column :actor_type_id, :integer, :null => false
- table.column :project_version_id, :integer, :null => false
- table.column :position, :integer, :null => false
- table.column :created_at, :datetime
- table.column :updated_at, :datetime
- table.column :lock_version, :integer, :null => false, :default => 0
- end
- add_index :actors, :project_version_id
- add_index :actors, :lock_version
- add_index :actors, [:name, :project_version_id], :unique => true, :name => 'actors_nameproject_version_id_index'
- end
- def self.down
- drop_table :actors
- end
- end
- ##Model - actor.rb
- class Actor < ActiveRecord::Base
- validates_presence_of :name, :project_version_id, :position
- validates_length_of :name, :in => 1..255
- validates_uniqueness_of :name, :scope => :project_version_id
- validates_numericality_of :actor_type_id, :project_version_id, :position, :only_integer => true
- belongs_to :project_version
- belongs_to :actor_type
- acts_as_list :scope => :project_version
- end
- ##MySQL info
- mysql> describe actors;
- +--------------------+--------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +--------------------+--------------+------+-----+---------+----------------+
- | id | int(11) | NO | PRI | NULL | auto_increment |
- | name | varchar(255) | NO | MUL | NULL | |
- | actor_type_id | int(11) | NO | | NULL | |
- | project_version_id | int(11) | NO | MUL | NULL | |
- | position | int(11) | NO | | NULL | |
- | created_at | datetime | YES | | NULL | |
- | updated_at | datetime | YES | | NULL | |
- | lock_version | int(11) | NO | MUL | 0 | |
- +--------------------+--------------+------+-----+---------+----------------+
- 8 rows in set (0.01 sec)
- ##Test - actor_test.rb
- def test_creation
- actor = Actor.new()
- assert !actor.valid? #passes
- assert actor.errors.invalid?(:name) #passes
- assert actor.errors.invalid?(:position) #fails
- assert actor.errors.invalid?(:project_version_id) #fails
- assert actor.errors.invalid?(:actor_type_id) #fails
- end
Add Comment
Please, Sign In to add comment