Porting SQL to Rails - Controllers and Views


I’ve run into a slight problem with extracint data from a table where
one field matches that of the same field of another table.

In my user controller I have:

def login
    @fname = params[:fname]
    @lname = params[:lname]

And I have this line at the top of the form in the login.rhtml file:

<%= start_form_tag :action => 'showbookings' :id => @fname, @lname %>

In the bookings controller I will have a def showbookings, and a
corresponding showbookings.rhtml file.

What I want to do is show all bookings (contents of the records) where
the fname and lname column match user.fname and user.lname (from the
users table).

I can do it in SQL:
SELECT * FROM Bookings
WHERE fname = users.fname AND lname = users.lname

But I’m not sure how to implement this in Rails, both as a controller
method and to output the info.

I appreciate the help.


Hello again,

I got this far:
In showbookings.rhtml

<p>Your Bookings</p>

  <% @user.bookings.each do |bookings| %>
  <%= bookings.from %>
  <%= bookings.to %>
  <%= bookings.dep_date %>
  <%= bookings.class %>
  <% end %>

<p>Your Flights</p>

  <% @bookings.flights.each do |flights| %>
  <%= flights.from %>
  <%= flights.destination %>
  <%= flights.dep_time %>
  <%= flights.arr_time %>
  <%= flights.duration %>
  <%= flights.class %>
  <% end %>

Can’t work how to link to match the fname and lname between users and
bookings, and then to match from and to from bookings to flights (second
ruby block).


  1. fname and lname are probably not the best identifiers for logging in
    because of the chance for duplication. Then I have to log in as Bob
    Smith2 or Jane Doel337 in order to be unique. How about a unique email

  2. I would set up your models like this (pseudocode):

has many flights through reservations conditions “confirmed = TRUE”

Reservations (or Bookings)
belongs to users, flights

has many users through reservations conditions “confirmed = TRUE”

Then it is easy to access users from flights with @flight.users or