Created
May 9, 2011 16:42
-
-
Save pjmagee/962847 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 %> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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