Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## create_people.rb
- class CreatePeople < ActiveRecord::Migration
- def self.up
- create_table :people, :force => true do |t|
- t.column :username, :string
- t.column :log_id, :integer
- t.column :last_login, :datetime
- t.column :access_level_id, :integer
- end
- execute "alter table people add constraint fk_person_log " +
- "foreign key (log_id) references logs(id)"
- execute "alter table people add constraint fk_person_access_level " +
- "foreign key (access_level_id) references access_levels(id)"
- # --- Now that people table is created, add foreign keys for logs table
- execute "alter table logs add constraint fk_log_created_by_person " +
- "foreign key (created_by_id) references people(id)"
- execute "alter table logs add constraint fk_log_updated_by_person " +
- "foreign key (last_updated_by_id) references people(id)"
- end
- def self.down
- drop_table :people
- end
- end
- ## create_logs.rb
- class CreateLogs < ActiveRecord::Migration
- def self.up
- create_table :logs, :force => true do |t|
- t.column :created_on, :datetime
- t.column :updated_on, :datetime
- t.column :created_by_id, :integer
- t.column :last_updated_by_id, :integer
- t.column :last_ip_address, :string
- end
- end
- def self.down
- drop_table :logs
- end
- end
- ## create_access_levels.rb
- class CreateAccessLevels < ActiveRecord::Migration
- def self.up
- create_table :access_levels do |t|
- t.column :name, :string
- end
- end
- def self.down
- drop_table :access_levels
- end
- end
- ## person.rb
- class Person < ActiveRecord::Base
- belongs_to :created_log, :class_name => 'Log', :foreign_key => "created_by_id"
- belongs_to :updated_log, :class_name => 'Log', :foreign_key => "last_updated_by_id"
- has_one :log
- has_one :access_level
- end
- ## log.rb
- class Log < ActiveRecord::Base
- belongs_to :person
- has_one :creator, :class_name => 'Person', :foreign_key => 'created_by_id'
- has_one :updater, :class_name => 'Person', :foreign_key => 'last_updated_by_id'
- end
- ## My Description: [plaintext]
- The tricky part that I think I am having problems with is the relationships between people and logs.
- Each person has a log and each log has both a creator and a updater.
- Does the code in person.rb and log.rb make sense with this kind of relationship.
Add Comment
Please, Sign In to add comment