Ruby on Rails

It Begins

What’s a girl to do when she is laid off from the position she’s had for over 14 years? Learn something new! Since I’m already familiar with basic web development I’ve decided to take it to the next level. This week I start learning Ruby on Rails.

My initial thought was to create a virtual machine on my laptop and install everything there, but after looking into all the elements that go into Rails I turned to Cloud9, where I could use clean installation with all the bells and whistles without having to deal with hosting concerns.

The tutorial I’m working through is based on a MySQL database, while the Cloud9  environment uses SQLite3. I’ve ran into a couple issues, but so far I’ve been able to track down the variations needed to run statements successfully. So far I’ve covered:

  • Creating a project by creating the tables, models and migrate files:
    [code]rails generate model MyTable[/code]
    Check status by running:[code]rake db:migrate:status[/code]
  • Generating controllers and views
  • Routes
  • Generating migrations

Working with Records

  • Use DESTROY instead of DELETE.  That way the entry becomes  frozen Hash, which means you can’t update it but you are able to refer to it.
  • Methods to Find Records
    Table.all returns an array of objects
    Table.first, Table.last
    Table.find(2) uses the primary key
    Dynamic finders include Table.find_by_id(2) or Table.find_by_column(“Some Field Value”)

ActiveRelation Queries

Allows manipulation of records given specific conditions. [emphasis type=”danger”]Important Note[/emphasis] [wp-svg-icons icon=”point-down” wrap=”span”]
[container]
[row]
[column md=”6″]SQL-InjectionStrings should be hard coded to avoid SQL Injection [/column]
[column md=”6″]

EscapedSQLThis query gives rails the opportunity to escape those values before it inserts them [/column]
[/row]
[/container]

Query Methods

  • order (sort order ASC or DESC)
  • limit (return no more than this many records)
  • offset (skip this many records)

[code]Table.order(“column ASC”).limit(20).offset(40)[/code]

Named Scopes

    • Queries defined in a model
    • Defined using ActiveRelation query methods
    • Can accept parameters
    • Rails 4 requires lambda syntax

[code]class Subject < ActiveRecord::Base
scope :visible, lambda { where(:visible => true)}
scope :invisible, lambda { where(:visible => false)}
scope :sorted, lambda { order(“subjects.position ASC”) }
scope :newest_first, lambda { order(“subjects.created_at DESC”)}
scope :search, lambda {|query|
where([“name LIKE ?”, “%#{query}%”])
}
end
[/code]


[wp-svg-icons icon=”busy” wrap=”span”]   I figure I’ve got 20 weeks of unemployment, so that should give me plenty of time to get the basics down.

Payment Methods & Best Practices

Customer Engagement & Marketing

  • Google Trusted Stores
  • Trusted Reviews
  • eBay
  • Amazon Seller Central