Ruby on Rails - Query JOINS on object with several conditions
===

RoR beginner here using rails 3.2.3 and ruby 1.9.3

Currently I am trying to show the availabilities of hotels that match the dates selected bt the user on two date_times.


        scope :by_availabilities, lambda { |first_day, last_day|
          joins(:rooms).
          joins('INNER JOIN availabilities ON rooms.id=availabilities.room_id').
          where(get_my_where(first_day, last_day)).
          group('hotels.id'<other fields>
        }

    def self.get_my_where(first_day, last_day)
            difference = (last_day-first_day).to_i
    
            condition = ""
    
    
            if (difference==1)
              condition = "availabilities.day = '#{first_day}'"
              condition+=""
            else
    
                for i in 1..difference do
                  if(i==difference)
                      condition +="(availabilities.day = '#{first_day}' AND availabilities.availability > 0)"
                  else
                    condition+="(availabilities.day = '#{first_day+i}' AND availabilities.availability > 0) AND "
                  end
                
                end
            end
    --other innerjoins--

The first_day is the in date and last_day is the out date, both are correclty declared and in a correct Date format.

I am passing the params correctly, the <u>issue is</ul> I am only getting a correct search result if I select an in date of for eg today and an out date for tommorow. If I put more days in between, the search gives me no results at all, even though there are availabilities in the DB for those days.

Is this not the way to perform this? I can't put this search on the controller as there are  many INNER JOINS and it was getting "ugly", and all the searches by other filters worked correctly until I implemented this one.

Thanks in advance