Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Raul Grigoriu
Raul Grigoriu
1,060 Points

def available? end

hello guys, I really need some help in making a method like def available?

some code...

end I have two models in a rails app cats for rent and booking i am trying to define a method that would check if the cat model is available for rent i have for booking atributes :starts_at & ends_at anybody can help me out on this one ? much apreciated thx

1 Answer

I've made some assumptions here.

Cat has many bookings Booking belongs to Cat

starts_at and ends_at are instances of the Time class.

This function should return true or false depending on whether the Cat is available or not.

def self.available?(booking_start_time, booking_end_time)
    query_start = booking_start_time.to_i
    query_end = booking_end_time.to_i
    self.bookings.each do |booking|
        start = booking.starts_at.to_i
        finish = booking.ends_at.to_i
        range = (start..finish)
        if range.include?(query_start) || range.include?(query_end)
            return false
        end
    end
    return true
end
Raul Grigoriu
Raul Grigoriu
1,060 Points

Ken, thank you very much for your answer, and I am terribly sorry for the so late reply...