Getting started with Ruby on Rails building a full-stack web app Part 1

Peter Ayeni
16 min readNov 26, 2019

--

Build a full-stack web application called DaCollector that help user keeps track of arts and interesting photos you find online.

If you have been following my blog for some weeks now, I have been writing a lot about Ruby, and web development with Ruby, HTML and CSS. We have also built a simple Todo App with Sinatra a lightweight Ruby web development library. If you are visiting my blog for the first time you can check out the previous contents.

In this series, I am going to be introducing Ruby on Rails while building a web application that allows the user to collect art and interesting pictures on the web.

A case for Ruby on Rails

Learning to build a modern web application is daunting. Ruby on Rails makes it much easier and more fun. It includes everything you need to build fantastic applications, and you can learn it with the support of our large, friendly community.

Imagine what you could build if you learned Ruby on Rails…

You’ve probably already used many of the applications that were built with Ruby on Rails: Basecamp, GitHub, Shopify, Airbnb, Twitch, SoundCloud, Hulu, Zendesk, Square, Cookpad. Those are just some of the big names, but there are literally hundreds of thousands of applications built with the framework since its release in 2004.

— Ruby Official Website

If the above doesn’t sell Ruby on Rails for you I don’t know what else with. With the reign of new Library and Framework coming out every week, making a choice of what to learn for web development can be a daunting task, Ruby on Rails is a mature framework that is battle-tested with a lot of community support and resources to get started as a beginner.

Data Modelling and ActiveRecord in Ruby on Rails

At the core of every web application is a database to store all the data related in our web application, database development is a huge topic and one of the heavy lifting Ruby on Rails did for us is give us out of the box a convenient way to work with Database without writing a single line of SQL or fully understand how Database work, all will need to is use ActiveRecord a Ruby on Rails module that helps us with all our database operations. And that is what we are are going to be focusing on in this part. So let get started.

I will assume you already have Ruby Install if not check my other tutorials or just Google Around installing Ruby you will see tutorials on how to install Ruby for your machine. After then we need to install Ruby on Rails just run gem install rails Rails SQLite as a default database for development and I will be using that also. All you need to do is make sure you have the latest SQLite install and Rails will handle the rest.

We will generate a new Rails application first install Node JS if you don’t already have and also install Yarn for Mac user just run brew install yarn for window, users visit this link to download and install Yarn. Yarn is a javascript package manager and Rails needs it for some JS business we will not go into. This will help you avoid some err during Rails installation.

run rails new dacollector

➜  code rails new dacollector
create
create README.md
create Rakefile
create .ruby-version
create config.ru
create .gitignore
create Gemfile
run git init from "."
Initialized empty Git repository in /Users/peterayeni/Development/code/dacollector/.git/
create package.json
create app
create app/assets/config/manifest.js
create app/assets/stylesheets/application.css
create app/channels/application_cable/channel.rb
create app/channels/application_cable/connection.rb
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/javascript/channels/consumer.js
create app/javascript/channels/index.js
create app/javascript/packs/application.js
create app/jobs/application_job.rb
create app/mailers/application_mailer.rb
create app/models/application_record.rb
create app/views/layouts/application.html.erb
create app/views/layouts/mailer.html.erb
create app/views/layouts/mailer.text.erb
create app/assets/images/.keep
create app/controllers/concerns/.keep
create app/models/concerns/.keep
create bin
create bin/rails
create bin/rake
create bin/setup
create bin/yarn
create config
create config/routes.rb
create config/application.rb
create config/environment.rb
create config/cable.yml
create config/puma.rb
create config/spring.rb
create config/storage.yml
create config/environments
create config/environments/development.rb
create config/environments/production.rb
create config/environments/test.rb
create config/initializers
create config/initializers/application_controller_renderer.rb
create config/initializers/assets.rb
create config/initializers/backtrace_silencers.rb
create config/initializers/content_security_policy.rb
create config/initializers/cookies_serializer.rb
create config/initializers/cors.rb
create config/initializers/filter_parameter_logging.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/new_framework_defaults_6_0.rb
create config/initializers/wrap_parameters.rb
create config/locales
create config/locales/en.yml
create config/master.key
append .gitignore
create config/boot.rb
create config/database.yml
create db
create db/seeds.rb
create lib
create lib/tasks
create lib/tasks/.keep
create lib/assets
create lib/assets/.keep
create log
create log/.keep
create public
create public/404.html
create public/422.html
create public/500.html
create public/apple-touch-icon-precomposed.png
create public/apple-touch-icon.png
create public/favicon.ico
create public/robots.txt
create tmp
create tmp/.keep
create tmp/pids
create tmp/pids/.keep
create tmp/cache
create tmp/cache/assets
create vendor
create vendor/.keep
create test/fixtures
create test/fixtures/.keep
create test/fixtures/files
create test/fixtures/files/.keep
create test/controllers
create test/controllers/.keep
create test/mailers
create test/mailers/.keep
create test/models
create test/models/.keep
create test/helpers
create test/helpers/.keep
create test/integration
create test/integration/.keep
create test/channels/application_cable/connection_test.rb
create test/test_helper.rb
create test/system
create test/system/.keep
create test/application_system_test_case.rb
create storage
create storage/.keep
create tmp/storage
create tmp/storage/.keep
remove config/initializers/cors.rb
remove config/initializers/new_framework_defaults_6_0.rb
run bundle install
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.

As you can see that Rails have already downloaded a lot of boilerplate codes and structure for us. Rails work with the principle of Convention over Configuration, Rails take off the repetitive task in web development and help you handle them while you focus on building your web application. cd dacollector and open the directory in your favourite editor I will be using VS Code, check out my previous post on how to supercharge VS Code for Ruby development. Try and poke around these files and see what is available for you. We are going to be working mostly in the app directory.

So start the Rails server run rails s

=> Booting Puma
=> Rails 6.0.1 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.0 (ruby 2.6.1-p33), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://127.0.0.1:3000
* Listening on tcp://[::1]:3000
Use Ctrl-C to stop

This will start the Rails Server on localhost port 300 visits the URL on your browser and yeee!!! you just spin up your very first Rails App. Easy Peezy!

So what are we building: We are modelling an Online Art Collector Platform where User can create Collection and also add Reviews to Collection. So what kind of data do we need for this kind of application

User
- Name
- First Name
- Last Name
- Email

Above is the information we need to store for our User model. For all of this tutorial, we will be working in the terminal using the Rails Console, and Interactive terminal that allows us to interact with Rails classes. So let get started with the User model. Open a new terminal and navigate to the dacollector directory or just cntr c to stop the server. Rails come with inbuilt generators that help us generate models, Rails generators can even generate more powerful things for us. Run rails g model name:string first_name:string last_name:string email:string

rails g model User name:string first_name:string last_name:string email:string
Running via Spring preloader in process 93471
invoke active_record
create db/migrate/20191126005953_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml

We tell rails to generate a user model and will pass it the attributes and the datatypes. You can see all the files Rails generated for us we have a migration file read more about Migration here. Migration is the only way we will be making changes to our database. You will see how that is done soon.

Now it’s time to run our migration rake db:migrate I talked about rake in my Sinatra tutorial.

rake db:migrate
== 20191126005953 CreateUsers: migrating ======================================
-- create_table(:users)
-> 0.0016s
== 20191126005953 CreateUsers: migrated (0.0017s) =============================

Running our migration create the users' table we can see that in the db folder a schema file is created and automatically updated, you will never need to touch this file. It is the reflection of our migrations.

ActiveRecord::Schema.define(version: 2019_11_26_005953) docreate_table "users", force: :cascade do |t|t.string "name"t.string "first_name"t.string "last_name"t.string "email"t.datetime "created_at", precision: 6, null: falset.datetime "updated_at", precision: 6, null: falseendend

That’s our Schema as you can see that there is an error in our model we have name, first_name and last_name, we need to get read of name it an error I introduce to show us how migration work. So how do we fix our database we will have to create a new migration to remove the name column.

rails g migration RemoveNameFromUsers Running via Spring preloader in process 93903
invoke active_record
create db/migrate/20191126011348_remove_name_from_users.rb

This command creates a new migration then we just need to open it up and had the following to it.

class RemoveNameFromUsers < ActiveRecord::Migration[6.0]  def change    remove_column :users, :name endend

Then we run rake db:migrate to run the migration again now you will see that our Schema as been automatic’s updated. And if you have SQLite Browser you will see that our database table has been updated accordingly powerful right, and we have just barely scratched the surface.

We have two more models to create let us do that. Now to the Collections Model, we want each user to be able to create Many collections, so a collection will belong to a user. To model, this relationship will a user foreign key in our Collection Model.

Collection
- Name
- URL
- Description
- User_ID

rails g model collections name:string url:string description:text user_id:integer

rails g model collections name:string url:string description:text user_id:integer
Running via Spring preloader in process 94006
[WARNING] The model name 'collections' was recognized as a plural, using the singular 'collection' instead. Override with --force-plural or setup custom inflection rules for this noun before running the generator.
invoke active_record
create db/migrate/20191126012423_create_collections.rb
create app/models/collection.rb
invoke test_unit
create test/models/collection_test.rb
create test/fixtures/collections.yml

The run the migration

rake db:migrate== 20191126011348 RemoveNameFromUsers: migrating ==============================
== 20191126011348 RemoveNameFromUsers: migrated (0.0000s) =====================
== 20191126012423 CreateCollections: migrating ================================
-- create_table(:collections)
-> 0.0022s
== 20191126012423 CreateCollections: migrated (0.0024s) =======================

The Review Model: For the Review we want a user to be able to give ratings to many collections including their own and also a Collection can have many reviews and a review belong to a collection.

Review
- Rating
- User_ID
- Collection_ID

rails g model reviews rating:float user_id:integer collection_id:integer
Running via Spring preloader in process 94083
[WARNING] The model name 'reviews' was recognized as a plural, using the singular 'review' instead. Override with --force-plural or setup custom inflection rules for this noun before running the generator.
invoke active_record
create db/migrate/20191126012944_create_reviews.rb
create app/models/review.rb
invoke test_unit
create test/models/review_test.rb
create test/fixtures/reviews.yml

Then run migration as we always do

rake db:migrate== 20191126012944 CreateReviews: migrating ====================================
-- create_table(:reviews)
-> 0.0017s
== 20191126012944 CreateReviews: migrated (0.0017s) ===========================

Now we have all our models created. Is time to let Rails no about the relationship between our models. At this point, I want to make things really simple I will be using the has many belongs to so we can get how relationship work and you can read more about Active Record Relationship on Rails Official Guide.

In the app/models directory open user.rb

class User < ApplicationRecord  has_many :collectionsend

And collection.rb

class Collection < ApplicationRecord  belongs_to :userend

Our User Collection Relationship is complete, so how do we test this out. Rails provide a seed file we can use to prepopulate our database in the db directory. just like I mention in our Sinatra tutorial that rake is a task runner that helps us run predefined tasks with Rails there are even more power with rake. if you run rake -T you will see all the list of rake tasks you can run in a Rails project.

rake -Trake about                              # List versions of all Rails frameworks and th...
rake action_mailbox:ingress:exim # Relay an inbound email from Exim to Action M...
rake action_mailbox:ingress:postfix # Relay an inbound email from Postfix to Actio...
rake action_mailbox:ingress:qmail # Relay an inbound email from Qmail to Action ...
rake action_mailbox:install # Copy over the migration
rake action_text:install # Copy over the migration, stylesheet, and Jav...
rake active_storage:install # Copy over the migration needed to the applic...
rake app:template # Applies the template supplied by LOCATION=(/...
rake app:update # Update configs and some other initially gene...
rake assets:clean[keep] # Remove old compiled assets
rake assets:clobber # Remove compiled assets
rake assets:environment # Load asset compile environment
rake assets:precompile # Compile all the assets named in config.asset...
rake cache_digests:dependencies # Lookup first-level dependencies for TEMPLATE...
rake cache_digests:nested_dependencies # Lookup nested dependencies for TEMPLATE (lik...
rake db:create # Creates the database from DATABASE_URL or co...
rake db:drop # Drops the database from DATABASE_URL or conf...
rake db:environment:set # Set the environment value for the database
rake db:fixtures:load # Loads fixtures into the current environment'...
rake db:migrate # Migrate the database (options: VERSION=x, VE...
rake db:migrate:status # Display status of migrations
rake db:prepare # Runs setup if database does not exist, or ru...
rake db:rollback # Rolls the schema back to the previous versio...
rake db:schema:cache:clear # Clears a db/schema_cache.yml file
rake db:schema:cache:dump # Creates a db/schema_cache.yml file
rake db:schema:dump # Creates a db/schema.rb file that is portable...
rake db:schema:load # Loads a schema.rb file into the database
rake db:seed # Loads the seed data from db/seeds.rb
rake db:seed:replant # Truncates tables of each database for curren...
rake db:setup # Creates the database, loads the schema, and ...
rake db:structure:dump # Dumps the database structure to db/structure...
rake db:structure:load # Recreates the databases from the structure.s...
rake db:version # Retrieves the current schema version number
rake log:clear # Truncates all/specified *.log files in log/ ...
rake middleware # Prints out your Rack middleware stack
rake restart # Restart app by touching tmp/restart.txt
rake secret # Generate a cryptographically secure secret k...
rake stats # Report code statistics (KLOCs, etc) from the...
rake test # Runs all tests in test folder except system ...
rake test:db # Run tests quickly, but also reset db
rake test:system # Run system tests only
rake time:zones[country_or_offset] # List all time zones, list by two-letter coun...
rake tmp:clear # Clear cache, socket and screenshot files fro...
rake tmp:create # Creates tmp directories for cache, sockets, ...
rake webpacker # Lists all available tasks in Webpacker
rake webpacker:binstubs # Installs Webpacker binstubs in this application
rake webpacker:check_binstubs # Verifies that webpack & webpack-dev-server a...
rake webpacker:check_node # Verifies if Node.js is installed
rake webpacker:check_yarn # Verifies if Yarn is installed
rake webpacker:clean[keep] # Remove old compiled webpacks
rake webpacker:clobber # Remove the webpack compiled output directory
rake webpacker:compile # Compile JavaScript packs using webpack for p...
rake webpacker:info # Provide information on Webpacker's environment
rake webpacker:install # Install Webpacker in this application
rake webpacker:install:angular # Install everything needed for Angular
rake webpacker:install:coffee # Install everything needed for Coffee
rake webpacker:install:elm # Install everything needed for Elm
rake webpacker:install:erb # Install everything needed for Erb
rake webpacker:install:react # Install everything needed for React
rake webpacker:install:stimulus # Install everything needed for Stimulus
rake webpacker:install:svelte # Install everything needed for Svelte
rake webpacker:install:typescript # Install everything needed for Typescript
rake webpacker:install:vue # Install everything needed for Vue
rake webpacker:verify_install # Verifies if Webpacker is installed
rake webpacker:yarn_install # Support for older Rails versions
rake yarn:install # Install all JavaScript dependencies as speci...
rake zeitwerk:check # Checks project structure for Zeitwerk compat..

now run rails console

rails console
Running via Spring preloader in process 94340
Loading development environment (Rails 6.0.1)
2.6.1 :001 >

The Rails console give us a command-line interface to talk to Rails it works the same way to Ruby IRB. We will be testing our models here. You type exit to quit the Rails console, even better you can run the console in a Sandbox Environment so that all your changes will be reverse on exit. So exit and let’s fo that.

rails console --sandboxRunning via Spring preloader in process 94439
Loading development environment in sandbox (Rails 6.0.1)
Any modifications you make will be rolled back on exit
2.6.1 :001 >

So let’s create some Users

user1 = User.create(first_name: "Jon", last_name:"Doe", email:"jon@gmail.com")   (6.2ms)  begin transaction
(0.1ms) SAVEPOINT active_record_1
User Create (2.0ms) INSERT INTO "users" ("first_name", "last_name", "email", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["first_name", "Jon"], ["last_name", "Doe"], ["email", "jon@gmail.com"], ["created_at", "2019-11-26 01:50:55.576947"], ["updated_at", "2019-11-26 01:50:55.576947"]]
(0.1ms) RELEASE SAVEPOINT active_record_1
=> #<User id: 1, name: nil, first_name: "Jon", last_name: "Doe", email: "jon@gmail.com", created_at: "2019-11-26 01:50:55", updated_at: "2019-11-26 01:50:55">

In the above, we created a new User using create, create automatically create a new User for us you can also see the SQL generated for us behind the scene. create is an Active Record method that handles the creation of a new user without us writing any SQL or understands the ways it does it.

Bellow is another way of creating a user.

2.6.1 :002 > user2 = User.new => #<User id: nil, name: nil, first_name: nil, last_name: nil, email: nil, created_at: nil, updated_at: nil> 
2.6.1 :003 > user2.first_name = "Peter"
=> "Peter"
2.6.1 :004 > user2.last_name = "Ayeni"
=> "Ayeni"
2.6.1 :005 > user2.email = "ayeni@example.com"
=> "ayeni@example.com"
2.6.1 :006 > user2.save
(1.4ms) SAVEPOINT active_record_1
User Create (0.4ms) INSERT INTO "users" ("first_name", "last_name", "email", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["first_name", "Peter"], ["last_name", "Ayeni"], ["email", "ayeni@example.com"], ["created_at", "2019-11-26 01:53:28.311379"], ["updated_at", "2019-11-26 01:53:28.311379"]]
(0.1ms) RELEASE SAVEPOINT active_record_1
=> true
2.6.1 :007 >

As you can see we used the new method, the new method does not create a user until we call save on the instance of the user. Those are the differences between using new and create.

Let’s add some Collections

collection1 = Collection.create(name: "Londond Bridge", url: "londin.jpg", description:"Oldest bridge in UK", user_id: 1)   (0.2ms)  SAVEPOINT active_record_1
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Collection Create (1.1ms) INSERT INTO "collections" ("name", "url", "description", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["name", "Londond Bridge"], ["url", "londin.jpg"], ["description", "Oldest bridge in UK"], ["user_id", 1], ["created_at", "2019-11-26 02:01:05.778072"], ["updated_at", "2019-11-26 02:01:05.778072"]]
(0.0ms) RELEASE SAVEPOINT active_record_1
=> #<Collection id: 1, name: "Londond Bridge", url: "londin.jpg", description: "Oldest bridge in UK", user_id: 1, created_at: "2019-11-26 02:01:05", updated_at: "2019-11-26 02:01:05">
2.6.1 :008 >
collection2 = Collection.create(name: "Eko Bridge", url: "lagos.jpg", description:"Longest bridge in Nigeria", user_id: 1) (0.1ms) SAVEPOINT active_record_1
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Collection Create (0.6ms) INSERT INTO "collections" ("name", "url", "description", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["name", "Eko Bridge"], ["url", "lagos.jpg"], ["description", "Longest bridge in Nigeria"], ["user_id", 1], ["created_at", "2019-11-26 02:02:25.791765"], ["updated_at", "2019-11-26 02:02:25.791765"]]
(0.1ms) RELEASE SAVEPOINT active_record_1
=> #<Collection id: 2, name: "Eko Bridge", url: "lagos.jpg", description: "Longest bridge in Nigeria", user_id: 1, created_at: "2019-11-26 02:02:25", updated_at: "2019-11-26 02:02:25">
2.6.1 :009 >

Above I heard two collections for the user with an id of 1 let’s add a collection for the user with an id of 2.

collection3 = Collection.create(name: "Olumo Rock", url: "olumo.jpg", description:"Tallest mountain in Nigeria", user_id: 2)   (0.1ms)  SAVEPOINT active_record_1
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
Collection Create (0.1ms) INSERT INTO "collections" ("name", "url", "description", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["name", "Olumo Rock"], ["url", "olumo.jpg"], ["description", "Tallest mountain in Nigeria"], ["user_id", 2], ["created_at", "2019-11-26 02:05:13.647210"], ["updated_at", "2019-11-26 02:05:13.647210"]]
(0.0ms) RELEASE SAVEPOINT active_record_1
=> #<Collection id: 4, name: "Olumo Rock", url: "olumo.jpg", description: "Tallest mountain in Nigeria", user_id: 2, created_at: "2019-11-26 02:05:13", updated_at: "2019-11-26 02:05:13">
2.6.1 :011 >

With this, we can start to query our database.

2.6.1 :013 > User.all  User Load (1.2ms)  SELECT "users".* FROM "users" LIMIT ?  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<User id: 1, name: nil, first_name: "Jon", last_name: "Doe", email: "jon@gmail.com", created_at: "2019-11-26 01:50:55", updated_at: "2019-11-26 01:50:55">, #<User id: 2, name: nil, first_name: "Peter", last_name: "Ayeni", email: "ayeni@example.com", created_at: "2019-11-26 01:53:28", updated_at: "2019-11-26 01:53:28">]>
2.6.1 :014 > User.count (2.3ms) SELECT COUNT(*) FROM "users"
=> 2
2.6.1 :015 > User.first User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
=> #<User id: 1, name: nil, first_name: "Jon", last_name: "Doe", email: "jon@gmail.com", created_at: "2019-11-26 01:50:55", updated_at: "2019-11-26 01:50:55">
2.6.1 :016 > user1.last_name
=> "Doe"
2.6.1 :017 > user2.email
=> "ayeni@example.com"
2.6.1 :018 > User.last User Load (1.7ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<User id: 2, name: nil, first_name: "Peter", last_name: "Ayeni", email: "ayeni@example.com", created_at: "2019-11-26 01:53:28", updated_at: "2019-11-26 01:53:28">
2.6.1 :019 >
2.6.1 :020 > User.find(1)
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
=> #<User id: 1, name: nil, first_name: "Jon", last_name: "Doe", email: "jon@gmail.com", created_at: "2019-11-26 01:50:55", updated_at: "2019-11-26 01:50:55">
2.6.1 :021 >

Above are some queries that can be performed with Active Record and there are many more visits to the guide for more.

Let write some relationship Query

2.6.1 :021 > user1
=> #<User id: 1, name: nil, first_name: "Jon", last_name: "Doe", email: "jon@gmail.com", created_at: "2019-11-26 01:50:55", updated_at: "2019-11-26 01:50:55">
2.6.1 :022 > user1.collections
Collection Load (1.5ms) SELECT "collections".* FROM "collections" WHERE "collections"."user_id" = ? LIMIT ? [["user_id", 1], ["LIMIT", 11]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Collection id: 1, name: "Londond Bridge", url: "londin.jpg", description: "Oldest bridge in UK", user_id: 1, created_at: "2019-11-26 02:01:05", updated_at: "2019-11-26 02:01:05">, #<Collection id: 2, name: "Eko Bridge", url: "lagos.jpg", description: "Longest bridge in Nigeria", user_id: 1, created_at: "2019-11-26 02:02:25", updated_at: "2019-11-26 02:02:25">, #<Collection id: 5, name: "Eko Bridge", url: "lagos.jpg", description: "Longest bridge in Nigeria", user_id: 1, created_at: "2019-11-26 02:06:06", updated_at: "2019-11-26 02:06:06">]>
2.6.1 :023 >
2.6.1 :023 > user2
=> #<User id: 2, name: nil, first_name: "Peter", last_name: "Ayeni", email: "ayeni@example.com", created_at: "2019-11-26 01:53:28", updated_at: "2019-11-26 01:53:28">
2.6.1 :024 > user2.collections
Collection Load (0.1ms) SELECT "collections".* FROM "collections" WHERE "collections"."user_id" = ? LIMIT ? [["user_id", 2], ["LIMIT", 11]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Collection id: 3, name: "Olumo Rock", url: "olumo.jpg", description: "Tallest mountain in Nigeria", user_id: 2, created_at: "2019-11-26 02:04:51", updated_at: "2019-11-26 02:04:51">, #<Collection id: 4, name: "Olumo Rock", url: "olumo.jpg", description: "Tallest mountain in Nigeria", user_id: 2, created_at: "2019-11-26 02:05:13", updated_at: "2019-11-26 02:05:13">]>
2.6.1 :025 >

You can see how Active Record relationship allow us to all the collections created by a particular User. Active Record is so powerful we barely scratched the surface in this tutorial. Visit Rails Guide to learn more about it. Also, play with the model we just created and try out different operations and more complex queries.

This is the end of the part one, in the next part we will cover the Controller in Rails remember the MVC development architecture we discuss in the Sinatra tutorial that’s the Rails way of web development. In our next chapter, we will add the controller to our Model and see how they fit together and then the last part we complete it with the View.

Have any question and feedbacks feel free to drop it in the comment section.

See you.

--

--

Peter Ayeni
Peter Ayeni

Written by Peter Ayeni

Senior Frontend Engineer. I change the world by helping people get started in Tech and Social Entrepreneurship.

No responses yet