$ rails new pressie2
$ rails g scaffold Recipient first_name:string last_name:string email:string snail_mail:string
$ rake db:migrate
$ rails g scaffold Present present:string cost:float
$ rake db:migrate
$ 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.
recipient.rb
class Recipient
has_many :presents
end
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
(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