Skip to content

Instantly share code, notes, and snippets.

@jendiamond
Last active December 4, 2015 00:04
Show Gist options
  • Select an option

  • Save jendiamond/6bb3b7c62738d3743074 to your computer and use it in GitHub Desktop.

Select an option

Save jendiamond/6bb3b7c62738d3743074 to your computer and use it in GitHub Desktop.

Create a new Present app

$ rails new pressie2


Add a Recipient model

$ rails g scaffold Recipient first_name:string last_name:string email:string snail_mail:string

$ rake db:migrate


Add a Present model

$ rails g scaffold Present present:string cost:float

$ rake db:migrate


Add a PresentsRecipients join table to link the two models

$ rails g model PresentsRecipients recipient:references present:references

$ rake db:migrate

The join table connects the two models through their ids.

The keywords belongs_to_and_has_many determine the relationship between these models, and declares present_id as foreign key to Recipient model.


Add the associations to your models

recipient.rb

class Recipient
  has_many :presents
end
Notice that the belongs_to :recipient association is already added.

present.rb

class PresentsRecipient < ActiveRecord::Base
  belongs_to :recipient
  belongs_to :present
end

Add the has_and_belongs_to_many association to the Present and Recipient models:

class Present
  has_and_belongs_to_many :recipients
end

class Recipient has_and_belongs_to_many :presents end

Active Record supports the following database column types:

(These are the options you can use when you generate a field in a model.
ie: rails g scaffold Present name**:string** cost**:decimal** recipient**:references**)

  • integer
  • primary_key
  • decimal
  • float
  • boolean
  • binary
  • string
  • text
  • date
  • time
  • datetime
  • timestamp

For testing lets add some files to the app in the seed.db

10.times do |i|
  Recipient.create(first_name: "Friend", last_name: "#{i}", email: "friend#{i}@mail.com", snail_mail: "#{i} Aaron St. Los Angeles, CA 90026")
end

Recipient.all.each do |recipient|
  Present.create(name: "Present #{recipient.id}", cost: 12.50, recipient: recipient)
end

Run rake db:seed and your local database will be populated with ten recipients and ten gifts.


Let's look at the present index.html.erb view http://localhost:3000/presents

Right now now we only see the object_id of the recipient. Recipient:0x007ffb5662a210 <td><%= present.recipient %></td>

We need to change this to make it show the recipient's first and last name.

  • Create a recipient_full_name method in the Recipient model to concatenate the first and last name

app/models/recipient.rb

class Recipient < ActiveRecord::Base
  has_many :presents

  def full_name
    "#{first_name} #{last_name}"
  end
end

** In the app/views/presents/index.html.erb** Change this:
<td><%= present.recipient %></td>
to this:
<td><%= present.recipient.full_name %></td>


Let's make a landing page

create a directory called static_pages index.html.erb

<p id="notice"><%= notice %></p>

<h1>Recipients</h1>

<table>
  <tbody>
    <% @recipients.each do |recipient| %>
      <tr>
        <td><%= recipient.full_name %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Recipient', new_recipient_path %>


create static_pages controller def index

class StaticPagesController < ApplicationController
	def index
	  @recipients = Recipient.all
	end
end

Update your routes/config.rb file

root 'static_pages#index'


Push to Heroku

Before we push to Heroku we have to add Postres to our production environment and add the rails 12 factor gem.

Open your Gemfile. Find the sqlite3 gem:

gem 'sqlite3'

We are goin to move it into the development environment. Then we are going to have the production environment use postgres.
(Heroku uses postres which is why we are adding it.)

Notice the groups :development and :production.

There are three environments in Rails development production test

development - This is where you develop your code production - This is the code you push to a server like Heroku test - This is where you test your code

First delete the gem 'sqlite3' that is already in your Gemfile

Then put this in your Gemfile:

group :development do
  gem 'sqlite3'
end
group :production do
  gem 'pg'
end

Notice the words do and end means this is a block of code that belongs together.
All the gems you want to be ONLY in the developmentgroup need to be in between the do and end of the development block.

There may be other gems in your production or development groups. That is okay. Just add the ones you need above or below them. It doesn't matter where you put it as long as you put it between the do and the end.

There are other gems that are outside of these groups. That is okay too. They are available to all the groups.

Then run this in your terminal to setup your dependencies:

$ bundle install --without production

===

Add rails_12factor

Next, we need to add rails_12factor entry into our Gemfile to make our app available on Heroku.

This gem modifies the way Rails works to suit Heroku, for example Logging is updated and the configuration for static assets (your images, stylesheets and javascript files) is tweaked to work properly within Heroku’s systems.
https://github.com/heroku/rails_12factor/blob/master/README.md
heroku/rails_12factor#3

Please change the following in the Gemfile:

group :production do
  gem 'pg'
end

to

group :production do
  gem 'pg'
  gem 'rails_12factor'
end

Then run bundle again.

Run your git workflow again to commit all changes and push to Heroku.

Then run heroku run rake db:migrate in your terminal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment