$ rails new pressie
$ rails g scaffold Recipient first_name:string last_name:string email:string snail_mail:string
$ rake db:migrate
$ rails g scaffold Present name:string cost:decimal recipient:references
$ rake db:migrate
The recipient:references part of this command adds a belongs_to :recipient relationship in your Present model.
When this relationship is specified, ActiveRecord will assume that the foreign key is kept in the recipient_id column and it will use a model named Recipient to instantiate the specific user.
It will also add an index on the new recipient_id column.
The keywords belongs_to and has_many determine the relationship between these models and declare recipient_id as foreign key to Present model.
recipient.rb
class Recipient
has_many :presents
end
present.rb
class Present
belongs_to :recipient
end
(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 %>
class StaticPagesController < ApplicationController
def index
@recipients = Recipient.all
end
end
Update your routes/config.rb file
root 'static_pages#index'
Before we push to Heroku we have to add Postres to our production environment and add the rails 12 factor 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.)
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
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.
$ bundle install --without production
===
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