-
Create a new rails application called "SimpleQuotes" and initialize a blank git repository inside the app:
rails new SimpleQuotes
cd SimpleQuotes
git init
-
Add all the files generated by rails to the repo and verify that all files are staged with the command git status. Commit these files by typing to git and verify again with git status.
git add .
git commit -m 'Base rails app'
-
Checkout a new branch called "quote". We will merge this back into master later.
git checkout -b quote
-
Create the Quote model and scaffolding using the rails generator. (Substitute the correct values):
rails generate scaffold <Model> <attribute>:<data type>
Your model should be Quote, with body:string and source:string. When generating the scaffold, multiple attributes (with colon and data type) are separated by a single space.
-
Visit the Rails API documentation from your browser for reference.
-
Open your rails project in your preferred editor.
-
Open the config/database.yml file and change the name of the development database to match the following:
development:
# ... other stuff
database: db/quotes_dev.sqlite3
-
Go to the db/migrate directory and verify there is a file named <somenumber>_create_quotes.rb. Open this file and make sure the content is accurate.
Set the default value of source to "Anonymous". To do this, change the line with :source to
t.string :source, default: "Anonymous"
-
Migrate the database:
rails db:migrate
-
Commit your work with git. First type git status to see what needs to be staged. You will notice that in addition to the config/database.yml change you made, rails automatically edited your routes.rb file to include routing information for the new model you created.
git status
In addition, there are a number of new files related to the quotes scaffolding that need to be added to the project. Since these are all related, we will add them all at once to the staging level with git add .
git add .
Now commit these changes from staging to the repository by typing. Type git log to see a history of the repository changes.
git commit -m "Quote model scaffolded; db minor edits"
-
Start up the development web server (or click the "Browser" button on PaizaCloud
rails server
-
To view the application, open up the browser by clicking on the "Browser" button
-
Add a /quotes to the end of the URL to get to the main quotes page.
-
Use the web ui to enter in at least three quotes from the famous quotes database. Update one quote. Destroy one quote.
-
Now that the basic CRUD functionality is working and the app is populated with test data, let's merge it back to the master branch.
git checkout master
git merge quote
-
Now we will practice interacting with the database directly using the command line. The simplequotes database could be found under: db/quotes_dev.sqlite3
Using the command line, you can type rails db as a shortcut to open the database. Typing .quit will return you to the command line. Typing .mode column and .header on will make the output more readable.
-
Experiment with the interface by adding, deleting and searching for quotes using SQL. Do not forget to submit examples of these operations as screenshots (basically the SQL queries you used).
-
Create a new Rails application called "BookManager", switch directories (cd) into this Rails app from the command line. Create a git repository, add and commit the initial files with the commit message "Initial commit".
-
Create three models and the scaffolding from the command line using the command:
rails generate scaffold <ModelName> <attribute>:<data type>
Below are the details of each model:
Publisher
name (string)
Author
first_name (string)
last_name (string)
Book
title (string)
publisher (use 'references' since it is a FK to publishers)
proposal_date (date)
contract_date (date)
published_date (date)
units_sold (integer)
-
Create an additional model called BookAuthor using the rails generate model command. The attributes of this model are book:references and author:references.
We don't need a full set of views or a controller, just an associative entity to connect books and authors, so using a model generator is sufficient in this case.
After creating these models, migrate the database and save all this generated code to git.
-
Create and switch to a new branch in git called models. Open the Book model in your editor and add the following three relationships to that model:
belongs_to :publisher # Should already exist in the file
has_many :book_authors
has_many :authors, through: :book_authors
In addition, add the following validations:
validates_presence_of :title
validates_numericality_of :units_sold
For the units_sold, refer to the Rails API for the option that restricts the values to integers only.
Finally, add the following scope:
scope :alphabetical, -> { order('title') }
-
Go to the Publisher model and add the following relationship, scope, and validation:
has_many :books
scope :alphabetical, -> { order('name') }
validates_presence_of :name
-
Go to the Author model and add the following relationships:
has_many :book_authors
has_many :books, through: :book_authors
In addition, add the following validations, scopes and methods:
validates_presence_of :first_name, :last_name
scope :alphabetical, -> { order('last_name, first_name') }
def name
"#{last_name}, #{first_name}"
end
-
Go to the BookAuthor model and ensure the following relationships exist:
belongs_to :book
belongs_to :author
Once the model code is complete, start the server, be sure it is running and that there are no typos (common error) in your code by looking at the index pages of books, authors and publishers, which can be found at:
For PaizaCloud Installations
Go to the link provided and then check out:
GIVEN_URL/books
GIVEN_URL/publishers
GIVEN_URL/authors
For Local Rails Installations
http://localhost:3000/books
http://localhost:3000/publishers
http://localhost:3000/authors
If it's all good, commit these changes to your git repo and then merge these changes into the master branch. Reminder, that should look like git checkout master to get back to the master branch and git merge models to incorporate your most recent updates.
-
We are going to complete some of the missing validations in the Book model. Create and move to a new book branch with git checkout -b book.
Since Rails does not have built-in date validations, we are using the validates_timeliness gem; find the documentation for this gem by clicking on the link and scroll down the webpage to see some example uses.
First, we need to include the gem in our project. We do this by going to the Gemfile and adding in gem 'validates_timeliness'. Now run bundle update to make sure you have the gem on your machine. Lastly, it is necessary to also run the rails generate validates_timeliness:install command to have a couple of files generated for the project, so do so now to make sure that you have them.
-
Now that we have the validates_timeliness gem installed, add the following validations to the following fields in the Book model. Make sure to use bundle install so that the new gem is installed.All the validations below should be checked through rails console.
Proposal Date
- Add a validation so that the
proposal_date is a legitimate date and that it is either the current date or some time in the past. (The reason is you shouldn't be allowed to record a proposal you haven't yet received.)
Contract Date
-
Add a validation to contract_date to ensure that it is a legitimate date and that it is either the current date or some time in the past. (The reason is you shouldn't be allowed to record a contract you haven't yet signed.)
-
Also make sure that the contract_date is some time after the proposal_date as you can't sign contracts for books yet to be proposed.
-
Finally allow the contract_date to be blank as not all books we are tracking have contracts yet.
Published Date
-
Add a validation to published_date so that it is also a legitimate date and that it is either the current date or some time in the past.
-
Also make sure that the published_date is some time after the contract_date as you can't publish books without contracts.
-
Finally allow the published_date to be blank as not all books we are tracking are published yet.
Start (or restart) your rails server and verify that these validations work in the interface. Confirm this before continuing. Do not merge back into master.