T-12 to Course Start

Rails Tutorial

  • Validation
    • Is done in app/models/ , using the validates :thing_to_validate, quality: must_be_so method
      • will be reflected in the  thing_to_validate.valid? boolean method
    • Tests go in spec/models/
      • RSpec equivalent of the boolean is be_valid
    • Options to validates with
      • presence, length (maximum), format (with), uniqueness (case_sensitive)
        • in RSpec testing for uniqueness, create a duplicate easily via the method .dup
      • uniqueness
        • requests in quick succession, might not have the time to compare to each other
        • a unique id per row in the DB table is needed
          • when generating a model, a new migration is automatically created
          • if structure needs to be added to an existing model: rails g migration add_index_to_users_email
          • in the db/migrate/ within the change method:
            • add_index :users, :email, unique: true
            • i.e. add an index to the email column in the users table, but with a nuique id
            • also will help avoid a full-table scan when searching for emails
      • Callbacks
        • method invoked in the lifetime of an ActiveRecord object
        • Examples: before_save
  • Passwords with has_secure_password
    • password_digest column in DB
      • again, adding structure to an existing model: rails g migration add_password_digest_to_users password_digest:string
        • i.e. rails g migration migration_name attribute_we_create:its_type
        • ending with _to_users signals to Rails that it should add a column to the users table
          • add_column :users, :password_digest, :string is added to the change method in this migration (inside db/migrate/)
    • will automatically implement a password_confirmation method
      • password will be typed twice, the second typing matching the first is a virtual attribute that will need to be verified
    • added bcrypt gem to Gemfile

Random Ruby

  • if you pass no object to each, it returns an Enumerator object. Running something like count on an Enumerator will have much low memory usage than with not.
  • You can assign a class to a constant, and then treat that constant as the name of the class – a one time deal
  • strip removes trailing and leading white spaces in strings
  • more than one assignment in a line is fine: a = b = 2

Random RSpec

  • it can be substituted with specify if you think one or the other sounds more “natural”
  • let is technically called a memo-ization technique

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s