3 models, joining and nested queries

I have 3 models,

    class Host       ref: string       address: string,       name: string          primary_key='ref'          has_many :rooms, :class_name=>"Room", :foreign_key=>'host_ref'

    Class Room       ref :string       host_ref: string       capacity: integer          primary_key='ref'          has_many :bookings, :class_name=>"Booking", :foreign_key=>'room_ref'          belongs_to :host

You will need a foreign_key spec on the above.

    Class Booking       ref: string       room_ref :string       start_date: date       end_date: string

Why is end_date a string?

      number_of_guests :integer          primary_key='ref'          belongs_to :room

You will need a foreign_key spec here also.

Unless you are connecting to a legacy database that you /definitely/ cannot change then do not use unusual names for the keys, and do not use string types. With Rails your life will be much simpler if you stick to the Rails conventions.

Here one should be able to see the vacancies against each host. A person when he enters the start date, end date and number of persons to book for a room, he must get the list of hosts who have rooms vacant and number of vacancies. How can I write the query in rails?

If you cannot see how to write the complete query then, for a start, just do what you can in the query and do the rest in code. Make sure that your automated tests check out the results and the tests pass. Then, if necessary, you can refactor the code and queries to make it more efficient, secure in the knowledge that your tests will show that it is correct. If you cannot even see how to make a start then the first thing is to write the requirement in pseudo code (something like find hosts where ..and ..). Once you can write that down clearly and unambiguously then you should be well on the way towards the solution.

Colin