W3D3 - Associations & ActiveRecord
Rails - building your own app
ActiveRecord::Base - get a lot of free methods
Associations - writing associations between Model classes
MUC - Model View Controller Framework
Associations Exercises - ActiveRecord::Base
Build a tool that will take an arbitrarily-long URL and retun a shortened, random URL to the user
Made it to the bonus, yay!
rails g migration MigrationName
Create models/singularized_tablename.rb for class SingularizedTableName
Use create! since it throws an error if it fails, whereas create fails quietly)
rails c - Test out models in Pry using Rails console
Exit and re-enter rails c frequently to keep environment up to date
Make sure Postgres app is open/running!
Need to include following in Gemfile to run Rails console:
group :development do gem 'pry-rails' end
lib/db/schema.rb - will auto-update to show you current schema of database tables (columns, datatypes, indices, constraints)
CreateMyNameTable - will automatically include create table syntax
SQL create statement syntax
def change create table :shortened_urls do |t| t.string :long_url, null: false t.string :short_url, null: false t.integer :user_id, null: false t.timestamps end add_index :shortened_urls, :user_id add_index :shortened_urls, :short_url, unique: true end
add_index - adding an index will speed up search time, but take up more memory. Add indices if you expect to search using that column a lot, but don't index everything
Adding uniqueness constraint - must do this using an add_index ... unique: true line
add_index :table_name, [:column1, :column2], unique: true - will add uniqueness constraint on the combination of column1:column2
t.timestamps - adds created_at & updated_at timestamps, always include
drop_table :table_name - drops a table
add_column :table_name, :column_name, null: false - add column, can add more constraints to end\
has_many & belongs_to syntax
has_many :name_of_method, class_name: :NameOfOtherClass, foreign_key: :name_of_fk_pointing_to_me, primary_key: :name_of_my_pk belongs_to :name_of_method, class_name: :NameOfOtherClass, foreign_key: :name_of_my_fk, primary_key: :name_of_pk_on_other_table
fk/pk for a has_many should match the fk/pk for the corresponding belongs_to association in the other class
Can think of association_name == method_name
Default values for has_many/belongs_to if you only write has_many :name_of_method
class_name: SingularVersionOfAssociaionName
foreign_key: classname_id / myclassname_id
has_many :through / has_one :through
has_many :new_method_name, through: :association_name (2nd to last step), source: :method_to_be_called_on_what_through_returns
has_one :through` - same as has_many :through, but only returns the first row of data
find_by_sql(sql_query) - use when ActiveRecord's built in SQL-esque methods are not effective, the query you need is more complicated. Can use heredocs to write sql_query
DB tables - plural snake_case
Models - singular CamexlCase
Associations/methods - snake_case
Controllers - plural CamelCaseController
Views - snake_case mappings of controller action