Skip to content

Instantly share code, notes, and snippets.

@pjmagee
Created May 9, 2011 16:42
Show Gist options
  • Select an option

  • Save pjmagee/962847 to your computer and use it in GitHub Desktop.

Select an option

Save pjmagee/962847 to your computer and use it in GitHub Desktop.
def booking
session = Session.find(params[:id])
booking = Booking.create(:session_id => session)
@user.bookings << booking
redirect_to users_path
end
class Session < ActiveRecord::Base
has_many :bookings
has_many :users, :through => :bookings
end
class Booking < ActiveRecord::Base
belongs_to :user
belongs_to :session
validates :user_id, :presence => true
validates :session_id, :presence => true
end
class User < ActiveRecord::Base
has_many :bookings
belongs_to :sessions
end
<% @sessions.each do |session| %>
<h1>Session <%= session.id %></h2>
<p>Activity: <%= session.activity %></p>
<p>Slot: <%= session.slot %></p>
<p><%= link_to "Make Booking", booking_url(:id => session.id) %></p>
<p>Current Bookees: <%= session.users.count %></p>
<% end %>
Routes.rb
match '/sessions/:id/booking' => 'users#booking', :as => :booking
users index.html.erb
<% @users.each do |user| %>
<%= user.name %></br>
<%= user.email %></br>
<h1>Bookings</h1>
<ul>
<% user.bookings.each do |booking| %>
<li>Booking number: <%= booking.id %></li></br>
<ul>
<li>Session: <%= booking.session.id %></li>
<li>Activity: <%= booking.session.activity %></li>
<li>Slot: <%= booking.session.slot %></li><br/>
<li>Others who booked this session:</li>
<ul>
<% booking.session.users.each do |user| %>
<li><%= link_to user.name, user_path(user) %></li>
<% end %>
</ul>
</ul>
<% end %>
</ul>
<% end %>
@pjmagee

pjmagee commented May 9, 2011

Copy link
Copy Markdown
Author

create_table "bookings", :force => true do |t|
t.integer "user_id"
t.integer "session_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "sessions", :force => true do |t|
t.string "activity"
t.integer "slot"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
end

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