Before running, you should have the following installed on your machine:
- Ruby
- Rails
- Postgres
- MongoDB
- Create the rails application:
rails new rails-postgres-mongo
- Change
Gemfile
fromsqlite3
topg
so we will use our Postgres locally - Change
config/database.yml
to:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: rails_postgres_mongo_development
- Generate a new ActiveRecord model for User:
rails g model User
- Change
db/migrate/*_create_users.rb
to:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps null: false
end
end
end
- Run
rake db:create
- Run
rake db:migrate
- Run
rails console
:
> User.create!(name: “Chris”, email: “[email protected]”)
- Append to
Gemfile
:gem ‘mongoid’
- Run a Mongoid configuration installation script:
rails g mongoid:config
- Replace
config/application.rb
line 7 withBundler.require(:default, Rails.env)
- Run
rails g model Status
- Change
app/model/status.rb
to:
class Status
include Mongoid::Document
include Mongoid::Timestamps
field :user_id, type: Integer
field :status, type: String
end
- Define how
user.rb
will use status:
class User < ActiveRecord::Base
def status
Status.where(user_id: self.id).order_by({created_at: -1}).one
end
def status=(status)
Status.create!(user_id: self.id, status: status)
end
def statuses
Status.where(user_id: self.id).order_by({created_at: -1})
end
end
- Define how
status.rb
will use user:
class Status
include Mongoid::Document
include Mongoid::Timestamps
field :user_id, type: Integer
field :status, type: String
def user
User.find(self.user_id)
end
end
rackup
Change config/routes.rb
to:
Rails.application.routes.draw do
root “users#index”
end
rails g controller Users
Then, set the UsersController to:
class UsersController < ApplicationController
def index
@users = User
end
end
<h1>Users</h1>
<ul>
<%- @users.all.each do |user| %>
<li><%= user.name %> - <%= user.status.status %></li>
<%- end %>
</ul>
<!— Latest compiled and minified CSS —>
<link rel=“stylesheet” href=“https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css” integrity=“sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==“ crossorigin=“anonymous”>
<!— Optional theme —>
<link rel=“stylesheet” href=“https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css” integrity=“sha384-aUGj/X2zp5rLCbBxumKTCw2Z50WgIr1vs/PFN4praOTvYXWlVyh2UtNUU0KAUhAX” crossorigin=“anonymous”>
<!— Latest compiled and minified JavaScript —>
<script src=“https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js” integrity=“sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==“ crossorigin=“anonymous”></script>
Append this to your users#index form:
<%- form_for [:user, Status.new] do ||f| %>
<div class=‘form-input’>
<input type=‘text’ name=‘user’>
</div>
<div class=‘form-input’>
<input type=‘text’ name=‘status’>
</div>
<%- end %>
Add this your config/routes.rb
to this:
resources :users do
resources :statuses
end