ActiveRecord Flashcards
Base: Rails creates Ruby ________ files in db/migrate
Rails creates Ruby migration files in db/migrate
Base: Subclass _________::Base to create __________
Subclass ActiveRecord::Base to create persistence
Base: rake db:migrate does the _________ migration
rake db:migrate does the database migration
Rake –tasks
$ rake –tasks
(in /Users/beaty-admin/Documents/DU/4585/demo)
rake about # List versions of all Rails frameworks and the env…
rake assets:clean # Remove compiled assets
rake assets:precompile # Compile all the assets named in config.assets.pre…
rake db:create # Create the database from DATABASE_URL or config/d…
rake db:drop # Drops the database using DATABASE_URL or the curr…
rake db:fixtures:load # Load fixtures into the current environment’s data…
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE…
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (sp…
rake –tasks Continued
rake db:schema:dump # Create a db/schema.rb file that can be portably u…
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database, load the schema, and initial…
rake db:structure:dump # Dump the database structure to db/structure.sql
rake db:version # Retrieves the current schema version number
rake doc:app # Generate docs for the app – also available doc:r…
rake log:clear # Truncates all *.log files in log/ to zero bytes
rake middleware # Prints out your Rack middleware stack
rake notes # Enumerate all annotations (use notes:optimize, :f…
rake notes:custom # Enumerate a custom annotation, specify with ANNOT…
rake rails:template # Applies the template supplied by LOCATION=(/path/…
rake rails:update # Update configs and some other initially generated…
rake routes # Print out all defined routes in match order, with…
rake secret # Generate a cryptographically secure secret key (t…
rake stats # Report code statistics (KLOCs, etc) from the appl…
rake test # Runs test:units, test:functionals, test:integrati…
rake test:recent # Run tests for {:recent=>“test:prepare”} / Test re…
rake test:single # Run tests for {:single=>“test:prepare”}
rake test:uncommitted # Run tests for {:uncommitted=>“test:prepare”} / Te…
rake time:zones:all # Displays all time zones, also available: time:zon…
rake tmp:clear # Clear session, cache, and socket files from tmp/ …
rake tmp:create # Creates tmp directories for sessions, cache, sock…
Generated Additions:
•id
–Default primary ___
•id
–Default primary key
Generated Additions:
- created_at
- updated_at
- xxx_id
–Foreign keys
- created_at
- updated_at
- xxx_id
–Foreign keys
Primary keys:
•Can change
–self.primary_key = “column”
•Can change
–self.primary_key = “column”
Primary keys:
•Still use “id” to refer to primary key
•Still use “id” to refer to primary key
Relationships:
- Associations between ______
- One to ___
- One to ____
- Many to _____
- Associations between tables
- One to one
- One to many
- Many to many
One to One:
class Employee < ActiveRecord::Base
has_one :office
end
class Office < ActiveRecord::Base
belongs_to :employee
foreign key - employee_id
end
class Employee < ActiveRecord::Base
has_one :office
end
class Office < ActiveRecord::Base
belongs_to :employee
foreign key - employee_id
end
ERD:
Rule:
•The model that has the foreign key always has the belongs_to
•The model that has the foreign key always has the belongs_to
Generated Methods for belongs_to and has_one:
- other
- other=(other)
- build_other(attributes={})
- create_other(attributes={})
- create_other!(attributes={})
Generated Methods for belongs_to and has_one:
- other
- other=(other)
- build_other(attributes={})
- create_other(attributes={})
- create_other!(attributes={})
One to Many:
class Manager < ActiveRecord::Base
has_many :employees
end
class Employee < ActiveRecord::Base
belongs_to :manager
foreign key - manager_id
end
class Manager < ActiveRecord::Base
has_many :employees
end
class Employee < ActiveRecord::Base
belongs_to :manager
foreign key - manager_id
end
One to Many ERD:
So…
$ rails new advisorandstudent
$ cd advisorandstudent
$ rails generate scaffold Student name:string advisor:references
$ rails generate scaffold Advisor name:string
$ rails new advisorandstudent
$ cd advisorandstudent
$ rails generate scaffold Student name:string advisor:references
$ rails generate scaffold Advisor name:string
Modify:
- app/models/advisor.rb
- app/views/students/index.html.erb
- app/views/students/show.html.erb
- app/views/advisors/show.html.erb
- app/models/advisor.rb
- app/views/students/index.html.erb
- app/views/students/show.html.erb
- app/views/advisors/show.html.erb
Modify Advisor Model:
$ more app/models/advisor.rb
class Advisor < ActiveRecord::Base
has_many :students
end
$ more app/models/advisor.rb
class Advisor < ActiveRecord::Base
has_many :students
end
Modify Student Model (Maybe):
$ more app/models/student.rb
class Student < ActiveRecord::Base
belongs_to :advisor
end
$ more app/models/student.rb
class Student < ActiveRecord::Base
belongs_to :advisor
end